gpt4 book ai didi

android - 无法使用 Flutter 中的项目创建 ExpansionPanelList

转载 作者:IT王子 更新时间:2023-10-29 06:47:31 29 4
gpt4 key购买 nike

我是 Flutter 的新手,所以我正在努力学习。但我一直在创建一个带有 ExpansionPanel 的 ExpansionPanelList。就像标题所说的那样,所有这些都是在谷歌的 Flutter 中创建的。

到目前为止我的代码:

import 'package:flutter/material.dart';


class ShoppingBasket extends StatefulWidget {
@override
ShoppingBasketState createState() => new ShoppingBasketState();
}

class ShoppingBasketState extends State<ShoppingBasket> {

@override
Widget build(BuildContext context) {
return new ExpansionPanelList(
children: <ExpansionPanel>[
new ExpansionPanel(
headerBuilder: _headerBuilder,
body: new Container(
child: new Text("body"),
),
)
],
);
}


Widget _headerBuilder(BuildContext context, bool isExpanded) {
return new Text("headerBuilder");
}
}

但是当我打开应用程序时,调试器说:

Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1430 pos 12: 'hasSize': is not true.

最佳答案

听起来您需要将 ExpansionPanelList 放入 ListViewColumn 或其他一些不会强制它的容器中特定尺寸。

下面是扩展面板的使用示例。

screenshot

import 'package:flutter/material.dart';

class ShoppingBasket extends StatefulWidget {
@override
ShoppingBasketState createState() => new ShoppingBasketState();
}

class MyItem {
MyItem({ this.isExpanded: false, this.header, this.body });

bool isExpanded;
final String header;
final String body;
}

class ShoppingBasketState extends State<ShoppingBasket> {
List<MyItem> _items = <MyItem>[
new MyItem(header: 'header', body: 'body')
];

@override
Widget build(BuildContext context) {
return new ListView(
children: [
new ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
_items[index].isExpanded = !_items[index].isExpanded;
});
},
children: _items.map((MyItem item) {
return new ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return new Text(item.header);
},
isExpanded: item.isExpanded,
body: new Container(
child: new Text("body"),
),
);
}).toList(),
),
],
);
}
}

void main() {
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('ExpansionPanel Example'),
),
body: new ShoppingBasket(),
),
));
}

Flutter Gallery 有更详细的 expansion panels example .

关于android - 无法使用 Flutter 中的项目创建 ExpansionPanelList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45104182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com