flutter ExpansionTile 层级菜单的实现

发布时间:2020-10-07 22:08:50 作者:hosition
来源:脚本之家 阅读:353

开发环境

flutter ExpansionTile 层级菜单的实现

效果

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

如 每个省,市,县;

如 树木的病虫害;

flutter ExpansionTile 层级菜单的实现

flutter ExpansionTile 层级菜单的实现

关键代码

 @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<NameBean>(bean),
   title: Text(bean.name),
   children: bean.children.map<Widget>(_buildItem).toList(),
   leading: CircleAvatar(
    backgroundColor: Colors.green,
    child: Text(bean.name.substring(0,1),style: TextStyle(color: Colors.white),),
   ),
  );
 }

分析

1. ExpansionTile的使用

一般传入三个参数

key,title,children;

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
///  <https://material.io/guidelines/components/lists-controls.html>.
class ExpansionTile extends StatefulWidget {

上面一段是 ExpansionTile 的源码注释。
粗略一看会发现几个熟悉的字眼:ListView,ListTile
不错,实现层级菜单的效果,需要搭配使用ListView与ListTile,
上面贴的关键代码中 _buildItem()方法恰恰符合这一点,
当有子条目的时候返回ExpansionTile ,当没有子条目的时候返回 ListTile;

完整代码--->gihub

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Flutter如何实现菜单弹出框PopupMenuButton功能
  2. 使用Flutter怎么实现弹出菜单

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

flutter expansiontile 层级菜单

上一篇:Java数据结构之有效队列定义与用法示例

下一篇:解决layui-open关闭自身窗口的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》