Can't create ExpansionPanelList with Items in Flutter

后端 未结 2 447
鱼传尺愫
鱼传尺愫 2021-01-06 23:57

I\'m new to Flutter so i am trying to get into it. But I\'m hanging on creating an ExpansionPanelList with ExpansionPanels in it. And Like the title says all created in goog

2条回答
  •  时光说笑
    2021-01-07 00:17

    It sounds like you need to put your ExpansionPanelList into a ListView or Column or some other container that won't force it to be a particular size.

    Here is an example of expansion panel usage.

    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 {
      List _items = [
        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(),
        ),
      ));
    }
    

    The Flutter Gallery has a more detailed expansion panels example.

提交回复
热议问题