前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Flutter】ExpansionTile 可折叠列表

【Flutter】ExpansionTile 可折叠列表

作者头像
韩曙亮
发布2023-03-29 15:48:53
1.3K0
发布2023-03-29 15:48:53
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、ExpansionTile 构造方法


下面是 ExpansionTile 的构造方法 ;

其中 required this.title 是必须要设置的参数 ;

代码语言:javascript
复制
class ExpansionTile extends StatefulWidget {
  /// Creates a single-line [ListTile] with a trailing button that expands or collapses
  /// the tile to reveal or hide the [children]. The [initiallyExpanded] property must
  /// be non-null.
  const ExpansionTile({
    Key? key,
    this.leading,						// 标题左侧的 Widget 组件
    required this.title,				// 展示的列表标题 Widget 
    this.subtitle,						// 子标题 
    this.onExpansionChanged,			// 列表 展开/折叠 回调函数
    this.children = const <Widget>[],	// 列表展示时显示的 Widget 组件集合
    this.trailing,						// 标题右侧的 Widget 组件
    this.initiallyExpanded = false,		// 默认状态下是否展开 , 默认不展开 
    this.maintainState = false,
    this.tilePadding,
    this.expandedCrossAxisAlignment,
    this.expandedAlignment,
    this.childrenPadding,
    this.backgroundColor,				// 背景沿着
    this.collapsedBackgroundColor,
    this.textColor,
    this.collapsedTextColor,
    this.iconColor,
    this.collapsedIconColor,
  }) : assert(initiallyExpanded != null),
       assert(maintainState != null),
       assert(
       expandedCrossAxisAlignment != CrossAxisAlignment.baseline,
       'CrossAxisAlignment.baseline is not supported since the expanded children '
           'are aligned in a column, not a row. Try to use another constant.',
       ),
       super(key: key);
}

二、完整代码示例


代码语言:javascript
复制
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

const NAMES = {
  '三十六天罡' : [ '宋江', '卢俊义', '吴用', '公孙胜', '关胜' ],
  '七十二地煞' : [ '陈继真', '黄景元', '贾成', '呼颜', '鲁修德' ]
};



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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    /// 材料设计主题
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          /// 标题组件
          title: Text("ListView 示例"),
        ),

        /// 列表组件
        body: ListView(
          children: _buildList(),
        ),
      ),
    );
  }

  /// 创建列表 , 每个元素都是一个 ExpansionTile 组件
  List<Widget> _buildList(){
    List<Widget> widgets = [];
    NAMES.keys.forEach((key) {
      widgets.add(_generateExpansionTileWidget(key, NAMES[key]));
    });
    return widgets;
  }

  /// 生成 ExpansionTile 组件 , children 是 List<Widget> 组件
  Widget _generateExpansionTileWidget(tittle, List<String>? names){
    return ExpansionTile(
      title: Text(
        tittle,
        style: TextStyle(
          color: Colors.black54,
          fontSize: 20
        ),
      ),
      children: names!.map((name) => _generateWidget(name)).toList(),
    );
  }

  /// 生成 ExpansionTile 下的 ListView 的单个组件
  Widget _generateWidget(name){
    /// 使用该组件可以使宽度撑满
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        height: 80,
        //width: 80,
        margin: EdgeInsets.only(bottom: 5),
        //margin: EdgeInsets.only(right: 5),
        alignment: Alignment.center,
        decoration: BoxDecoration(color: Colors.black),
        child: Text(
          name,
          style: TextStyle(
              color: Colors.yellowAccent,
              fontSize: 20
          ),
        ),
      ),
    );
  }
}

执行效果 :

请添加图片描述
请添加图片描述

三、相关资源


参考资料 :

重要的专题 :

博客源码下载 :

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、ExpansionTile 构造方法
  • 二、完整代码示例
  • 三、相关资源
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档