ExpansionTile列表

在这里插入图片描述

main.dart

import 'package:flutter/material.dart';
import 'Expansion_panel_list.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: ExpansionPanelListDemo()
    );
  }
}

Expansion_panel_list

import 'package:flutter/material.dart';

class ExpansionPanelListDemo extends StatefulWidget {
  _ExpansionPanelListDemoState createState()=> _ExpansionPanelListDemoState();
}

// 定义扩展状态类

class ExpandStateBean{
  var isOpen;
  var index;
  ExpandStateBean(this.index,this.isOpen);
}


class _ExpansionPanelListDemoState extends State<ExpansionPanelListDemo>{
  var currentPanelIndex = -1;
  List<int> mList;// 组成一个int 类型的数组 ,用索引控制
  List<ExpandStateBean> expandStateList; // 开展开的状态列表,ExpandStateBean式自定义的类
  // 构造方法,调用这个类的时候自动执行
  _ExpansionPanelListDemoState(){
    mList = new List();
    expandStateList = new List();
    // 遍历集合进行赋值
    for(int i = 0; i<10; i++){
      mList.add(i);
      expandStateList.add(ExpandStateBean(i, false));
    }
  }
  // 修改展开与闭合的内部方法
 _setCurrentIndex(int index, isExpand){
    setState(() {
      // 遍历可展开状态列表
      expandStateList.forEach((element) {
        if(element.index == index){
          // 取反
          element.isOpen = !isExpand;
        }
      });
    });
 }

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("expansion panel list"),
      ),
      body: SingleChildScrollView(
        child: ExpansionPanelList(
          // 交互回调属性
          expansionCallback: (index,bol){
            _setCurrentIndex(index, bol);
          },
          children: mList.map((e){
            return ExpansionPanel(
              headerBuilder:(content,isExpanded){
                return ListTile(
                 title: Text('this is No $e')
                );
              },
              body: ListTile(
                title: Text('expansion no. $e'),
              ),
              isExpanded: expandStateList[e].isOpen
            );
          }).toList()
        ),
      ),
    );
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值