2

在这个简单的代码中,我试图ExpansionPanelListListView实现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);
}
4

1 回答 1

2

问题解决了

  @override
  void initState() {
    super.initState();
    List.generate(5, (int index) {
      items.add(NewItem(
        false,
        'aaaaa',
        Container(child:Text('1')),
        Container(child:Text('2')),
      ));
    });
  }
于 2019-08-08T08:27:52.000 回答