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

這篇文章將為大家詳細(xì)講解有關(guān)flutterExpansionTile層級(jí)菜單的實(shí)現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的龍文網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

開發(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)過程中的狀態(tài);

2. 遞歸

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

通過 bean.children.isEmpty 來結(jié)束遞歸;如 “直轄市”中的北京,下面沒有 “市”了,也就是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)沒有子條目的時(shí)候返回 ListTile;

關(guān)于flutterExpansionTile層級(jí)菜單的實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文名稱:flutterExpansionTile層級(jí)菜單的實(shí)現(xiàn)
網(wǎng)頁路徑:http://muchs.cn/article6/gdciig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、Google外貿(mào)建站、品牌網(wǎng)站制作、微信小程序、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司