无法使用Flutter中的Items创建ExpansionPanelList

问题描述 投票:0回答:2

我是Flutter的新手,所以我想进入它。但我正在创建一个包含ExpansionPanels的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");
  }
}

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

引发了另一个例外:'package:flutter / src / rendering / box.dart':断言失败:第1430行第12行:'hasSize':不是真的。

android panel flutter
2个回答
4
投票

听起来你需要将你的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


0
投票

还有另一种方法可以实现在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'),
                    )
                  ],
                )
              ],
            )
© www.soinside.com 2019 - 2024. All rights reserved.