Flutter ExpansionPanel不在ListView上扩展

时间:2019-08-08 07:56:52

标签: flutter flutter-layout flutter-listview

在这个简单的代码中,我尝试在ExpansionPanelList中加入ListView,在实现后,ExpansionPanel在单击以展开或折叠后不会展开

import 'package:flutter/material.dart';

void main()=>runApp(MaterialApp(home: App()));

class App extends StatefulWidget {
  @override
  State<StatefulWidget> createState()=>_App();
}

class _App extends State<App> {
  List<NewItem> items = [];
  @override
  Widget build(BuildContext context) {

    items=[];

    List.generate(5, (int index) {
      items.add(NewItem(
        false,
        'aaaaa',
        Container(child:Text('1')),
        Container(child:Text('2')),
      ));
    });

    return Scaffold(body: ListView(
      children: [
        new Padding(
          padding: new EdgeInsets.all(10.0),
          child: new ExpansionPanelList(
            expansionCallback: (int index, bool isExpanded) {
              setState(() {
                items[index].isExpanded = !items[index].isExpanded;
              });
            },
            children: items.map((NewItem item) {
              return new ExpansionPanel(
                headerBuilder: (BuildContext context, bool isExpanded) {
                  return new ListTile(
                      leading: item.image,
                      title: new Text(
                        item.header,
                        textAlign: TextAlign.right,
                        style: new TextStyle(
                          fontSize: 20.0,
                          fontWeight: FontWeight.w400,
                        ),
                      ));
                },
                isExpanded: item.isExpanded,
                body: item.body,
              );
            }).toList(),
          ),
        )
      ],
    ),);
  }
}

class NewItem {
  bool isExpanded;
  final String header;
  final Widget body;
  final Widget image;

  NewItem(this.isExpanded, this.header, this.body, this.image);
}

1 个答案:

答案 0 :(得分:2)

问题解决了

  @override
  void initState() {
    super.initState();
    List.generate(5, (int index) {
      items.add(NewItem(
        false,
        'aaaaa',
        Container(child:Text('1')),
        Container(child:Text('2')),
      ));
    });
  }