亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn)

 更新時(shí)間:2019年07月24日 15:52:16   作者:hosition  
這篇文章主要介紹了flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

開發(fā)環(huán)境

  • win10
  • Android Studio

效果

用于多級(jí)菜單展示,或選擇。

如 每個(gè)省,市,縣;

如 樹木的病蟲害;

關(guān)鍵代碼

 @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),),
   ),
  );
 }

分析

  • api:ExpansionTile
  • 算法:遞歸

1. ExpansionTile的使用

一般傳入三個(gè)參數(shù)

key,title,children;

  • title:每一行上面的文字;
  • children:菜單下面的子條目,是一個(gè)數(shù)組;
  • key:根據(jù)源碼傳入PageStorageKey,用于保存滑動(dòng)過(guò)程中的狀態(tài);

2. 遞歸

有的條目有子條目,有的沒(méi)有,通過(guò)遞歸的方式遍歷出每條數(shù)據(jù);

通過(guò) bean.children.isEmpty 來(lái)結(jié)束遞歸;
如 “直轄市”中的北京,下面沒(méi)有 “市”了,也就是children.isEmpty,此時(shí)應(yīng)該結(jié)束遞歸,返回 ListTile;
如“省級(jí)行政單位” 下面的 “黑龍江”還有很多個(gè)“市”,還不需要繼續(xù)遍歷返回 層級(jí)菜單ExpansionTile;

3. 源碼

學(xué)習(xí)flutter,很多不了解的地方都可以試著看看對(duì)應(yīng)源碼上面的注釋。

/// 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 的源碼注釋。
粗略一看會(huì)發(fā)現(xiàn)幾個(gè)熟悉的字眼:ListView,ListTile
不錯(cuò),實(shí)現(xiàn)層級(jí)菜單的效果,需要搭配使用ListView與ListTile,
上面貼的關(guān)鍵代碼中 _buildItem()方法恰恰符合這一點(diǎn),
當(dāng)有子條目的時(shí)候返回ExpansionTile ,當(dāng)沒(méi)有子條目的時(shí)候返回 ListTile;

完整代碼--->gihub

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論