android tile二级状态,flutter-ExpansionTile-层级菜单

开发环境

win10

Android Studio

bVbvtXX?w=636&h=275

效果

用于多级菜单展示,或选择。

如 每个省,市,县;

如 树木的病虫害;

bVbvt8X?w=1080&h=1920

bVbvt8Y?w=1080&h=1920

关键代码

@override

Widget build(BuildContext context) {

return ListTile(

title: _buildItem(widget.bean),

);

}

Widget _buildItem(NameBean bean){

if(bean.children.isEmpty){

return ListTile(

title: Text(bean.name),

onTap: (){

_showSeletedName(bean.name);

},

);

}

return ExpansionTile(

key: PageStorageKey(bean),

title: Text(bean.name),

children: bean.children.map(_buildItem).toList(),

leading: CircleAvatar(

backgroundColor: Colors.green,

child: Text(bean.name.substring(0,1),style: TextStyle(color: Colors.white),),

),

);

}

分析

api:ExpansionTile

算法:递归

1. ExpansionTile的使用

一般传入三个参数

key,title,children;

title:每一行上面的文字;

children:菜单下面的子条目,是一个数组;

key:根据源码传入PageStorageKey,用于保存滑动过程中的状态;

2. 递归

有的条目有子条目,有的没有,通过递归的方式遍历出每条数据;

通过 bean.children.isEmpty 来结束递归;

如 “直辖市”中的北京,下面没有 “市”了,也就是children.isEmpty,此时应该结束递归,返回 ListTile;

如“省级行政单位” 下面的 “黑龙江”还有很多个“市”,还不需要继续遍历返回 层级菜单ExpansionTile;

3. 源码

学习flutter,很多不了解的地方都可以试着看看对应源码上面的注释。

/// A single-line [ListTile] with a trailing button that expands or collapses

/// the tile to reveal or hide the [children].

///

/// This widget is typically used with [ListView] to create an

/// "expand / collapse" list entry. When used with scrolling widgets like

/// [ListView], a unique [PageStorageKey] must be specified to enable the

/// [ExpansionTile] to save and restore its expanded state when it is scrolled

/// in and out of view.

///

/// See also:

///

/// * [ListTile], useful for creating expansion tile [children] when the

/// expansion tile represents a sublist.

/// * The "Expand/collapse" section of

/// .

class ExpansionTile extends StatefulWidget {

上面一段是 ExpansionTile 的源码注释。

粗略一看会发现几个熟悉的字眼:ListView,ListTile

不错,实现层级菜单的效果,需要搭配使用ListView与ListTile,

上面贴的关键代码中 _buildItem()方法恰恰符合这一点,

当有子条目的时候返回ExpansionTile ,当没有子条目的时候返回 ListTile;

完整代码--->gihub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值