Flutter- ExpansionTile expand and collapse on click

后端 未结 3 1411
北荒
北荒 2020-12-10 07:06

I am taking this as a reference to expanding and collapsing of expansion tile-------Flutter - Collapsing ExpansionTile after choosing an item

What I want is if I one

3条回答
  •  庸人自扰
    2020-12-10 07:19

    You can use ExpandablePanelList to do perform expand and collapse view. Use expansionCallback to maintain active state to perform expand or collapse. here is the working example

    class TestExpandableView extends StatefulWidget {
      @override
      _TestExpandableViewState createState() => _TestExpandableViewState();
    }
    
    class _TestExpandableViewState extends State {
      int _activeMeterIndex;
      @override
      Widget build(BuildContext context) {
        return Container(
          child: new ListView.builder(
              itemCount:  2,
              itemBuilder: (BuildContext context, int i) {
                return Card(
                  margin:
                  const EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 0.0),
                  child: new ExpansionPanelList(
                    expansionCallback: (int index, bool status) {
                      setState(() {
                        _activeMeterIndex = _activeMeterIndex == i ? null : i;
                      });
                    },
                    children: [
                      new ExpansionPanel(
                          isExpanded: _activeMeterIndex == i,
                          headerBuilder: (BuildContext context,
                              bool isExpanded) =>
                          new Container(
                              padding:
                              const EdgeInsets.only(left: 15.0),
                              alignment: Alignment.centerLeft,
                              child: new Text(
                                'list-$i',
                              )),
                          body: new Container(child: new Text('content-$i'),),),
                    ],
                  ),
                );
              }),
        );
      }
    }
    

提交回复
热议问题