Flutter中顯示條件Widget的實(shí)現(xiàn)方式
Flutter 中如何顯示條件 Widget
1. 場(chǎng)景:
在 Flutter 日常開發(fā)中經(jīng)常會(huì)遇見(jiàn)這樣的需求,如: 只有用戶是 VIP 時(shí),才能展示某個(gè)入口或者某個(gè)模塊。這樣的需求在開發(fā)業(yè)務(wù)需求中多如牛毛,那你是如何來(lái)優(yōu)雅的實(shí)現(xiàn)的呢?
2. 推薦實(shí)現(xiàn)方式
下面是本人在開發(fā)中常用的集中實(shí)現(xiàn)方式,博友們可以根據(jù)自己的業(yè)務(wù)需求可以參考。
if 形式
這是一種非常常見(jiàn)的形式,滿足條件就實(shí)現(xiàn) Widget。示例如下:
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), if (_counter > 2) Container( width: 100, height: 100, color: Colors.red, ) ], ),
注意: 在 if 后面不能使用大括號(hào) ({})。 錯(cuò)誤指示如下:
if-else 形式
這也是一種常見(jiàn)的形式,滿足條件顯示 Widget1 ;不滿足條件顯示 Widget2 。示例如下:
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), if (_counter > 2) Container( width: 100, height: 100, color: Colors.red, ) else Container( width: 100, height: 100, color: Colors.green, ) ], )
注意: 在 if-else 后面不能使用大括號(hào) ({}); if 下的組件件的后面不能使用逗號(hào)(,)。 錯(cuò)誤寫法示例:
if...[widget1,widget2] 形式
該種形式也是常用于業(yè)務(wù)開發(fā),它是當(dāng)條件成立時(shí),顯示多個(gè) Widget。 示例如下:
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), if (_counter > 2) ...[ Container( width: 100, height: 100, color: Colors.red, ), Container( width: 100, height: 100, color: Colors.green, ) ], ], )
if...[widget1,widget2] else...[widget3,widget4] 形式
該種形式也是常用于業(yè)務(wù)開發(fā),它是當(dāng)條件成立時(shí),顯示多個(gè) Widget;條件不成立時(shí),顯示多個(gè) Widget。 示例如下:
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), if (_counter > 2) ...[ Container( width: 100, height: 100, color: Colors.red, ), Container( width: 100, height: 100, color: Colors.green, ) ] else ...[ Container( width: 100, height: 100, color: Colors.pinkAccent, ), Container( width: 100, height: 100, color: Colors.yellow, ), ] ], )
注意: if 下的組件集合的后面不能使用逗號(hào)(,)。 錯(cuò)誤寫法示例:
函數(shù)形式
這種形式是將這一塊的邏輯抽離到另一個(gè)地方。該方法有個(gè)不足之處,那就是在不滿足條件時(shí)也要返回一個(gè) Widget。 示例如下:
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), getWidget1(), ], ) // 函數(shù)形式 Widget getWidget1() { if (_counter > 2) { return Container( width: 100, height: 100, color: Colors.green, ); } else { return Container( width: 100, height: 100, color: Colors.pinkAccent, ); } } }
3. 總結(jié)
以上就是 Flutter 如何顯示條件 Widget 的方式。其實(shí)還有其他的方法,例如 switch 。這些其他的方法我們?cè)诤罄m(xù)文章中介紹。
以上就是Flutter中顯示條件Widget的實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Flutter顯示W(wǎng)idget的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android Jetpack組件之ViewModel使用詳解
Android中的ViewModel是一個(gè)可以用來(lái)存儲(chǔ)UI相關(guān)的數(shù)據(jù)的類。ViewModel的生命周期會(huì)比創(chuàng)建它的Activity、Fragment的生命周期長(zhǎng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-04-04android 上傳aar到私有maven服務(wù)器的示例
這篇文章主要介紹了android 上傳aar到私有maven服務(wù)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
這篇文章主要介紹了Android自定義ChronometerView實(shí)現(xiàn)類似秒表倒計(jì)時(shí),短信驗(yàn)證碼倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)短信、微信、微博分享功能
微信、微博分享功能大家都體驗(yàn)過(guò)吧,非常方便我們的生活,下面通過(guò)本文給大家介紹Android實(shí)現(xiàn)短信、微信、微博分享功能,需要的朋友參考下吧2017-12-12Android開發(fā)實(shí)戰(zhàn)之漂亮的ViewPager引導(dǎo)頁(yè)
這篇文章主要介紹了Android開發(fā)實(shí)戰(zhàn)中漂亮ViewPager引導(dǎo)頁(yè)的制作過(guò)程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android應(yīng)用內(nèi)調(diào)用第三方應(yīng)用的方法
這篇文章主要介紹了Android應(yīng)用內(nèi)調(diào)用第三方應(yīng)用的方法,有需要的朋友可以參考一下2014-01-01Android實(shí)現(xiàn)實(shí)時(shí)搜索框功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)實(shí)時(shí)搜索框功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Android RecyclerView加載不同布局簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView加載不同布局簡(jiǎn)單實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08