无法在Flutter中创建带有项目的ExpansionPanelList

Luk*_*ner 1 android panel flutter

我是Flutter的新手,所以我正尝试加入。但是我一直在用ExpansionPanels创建一个ExpansionPanelList。就像标题所说的一样,所有内容都是在Google 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");
  }
}
Run Code Online (Sandbox Code Playgroud)

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

引发了另一个异常:'package:flutter / src / rendering / box.dart':断言失败:1430行pos 12:'hasSize':不正确。

Muh*_*dil 8

还有另一种方法可以实现ExpansionTile在内部使用的相同用户体验ListView

         ListView(
              shrinkWrap: true,
              children: <Widget>[
                ExpansionTile(
                  leading: Icon(Icons.event),                         
                  title: Text('Test1'),
                  children: <Widget>[
                    ListTile(title: Text('Title of the item')),
                    ListTile(
                      title: Text('Title of the item2'),
                    )
                  ],
                ),
                ExpansionTile(
                  title: Text('Test2'),
                  children: <Widget>[
                    ListTile(title: Text('Title of the item')),
                    ListTile(
                      title: Text('Title of the item2'),
                    )
                  ],
                )
              ],
            )
Run Code Online (Sandbox Code Playgroud)


Col*_*son 5

听起来您需要将其ExpansionPanelList放入一个ListView或一个Column其他容器中,以免将其设置为特定大小。

这是扩展面板用法的示例。

屏幕截图

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(),
    ),
  ));
}
Run Code Online (Sandbox Code Playgroud)

Flutter Gallery有一个更详细的扩展面板示例