美文网首页Flutter项目实战
ExpansionPanel可伸缩列表的使用

ExpansionPanel可伸缩列表的使用

作者: 51bitquant | 来源:发表于2020-04-14 14:18 被阅读0次

    ExpansionPanel可伸缩列表的使用

    直接看代码。

    import 'package:flutter/material.dart';
    
    // stores ExpansionPanel state information
    class Item {
      Item({
        this.expandedValue,
        this.headerValue,
        this.isExpanded = false,
      });
    
      String expandedValue;
      String headerValue;
      bool isExpanded;
    }
    
    List<Item> generateItems(int numberOfItems) {
      return List.generate(numberOfItems, (int index) {
        return Item(
          headerValue: 'Panel $index',
          expandedValue: 'This is item number $index',
        );
      });
    }
    
    class MyStatefulWidget extends StatefulWidget {
      MyStatefulWidget({Key key}) : super(key: key);
    
      @override
      _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      List<Item> _data = generateItems(8);
    
      @override
      Widget build(BuildContext context) {
        return SingleChildScrollView(
          child: Container(
            child: _buildPanel(),
          ),
        );
      }
    
      Widget _buildPanel() {
        return ExpansionPanelList(
          expansionCallback: (int index, bool isExpanded) {
            setState(() {
              _data[index].isExpanded = !isExpanded;
            });
          },
          children: _data.map<ExpansionPanel>((Item item) {
            return ExpansionPanel(
              headerBuilder: (BuildContext context, bool isExpanded) {
                return ListTile(
                  title: Text(item.headerValue),
                );
              },
              body: ListTile(
                  title: Text(item.expandedValue),
                  subtitle: Text('To delete this panel, tap the trash can icon'),
                  trailing: Icon(Icons.delete),
                  onTap: () {
                    setState(() {
                      _data.removeWhere((currentItem) => item == currentItem);
                    });
                  }),
              isExpanded: item.isExpanded,
            );
          }).toList(),
        );
      }
    }
    
    

    the demo for how to using is as the follow, create a simple page for it, then use it as you want.

    class ExpansionDemoPage extends StatefulWidget {
    @override
    _ExpansionDemoPageState createState() => _ExpansionDemoPageState();
    }

    class _ExpansionDemoPageState extends State<ExpansionDemoPage> {
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text("Search Page"),
    ),
    body: MyStatefulWidget(),
    );
    }
    }

    then you will see how it display. 这是可伸缩列表的简单使用, 你可以自定义自己想要的控件。

    相关文章

      网友评论

        本文标题:ExpansionPanel可伸缩列表的使用

        本文链接:https://www.haomeiwen.com/subject/gszzmhtx.html