Flutter中显示条件Widget的实现方式
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
Flutter 中如何显示条件 Widget
1. 场景:
在 Flutter 日常开发中经常会遇见这样的需求,如: 只有用户是 VIP 时,才能展示某个入口或者某个模块。这样的需求在开发业务需求中多如牛毛,那你是如何来优雅的实现的呢?
2. 推荐实现方式
下面是本人在开发中常用的集中实现方式,博友们可以根据自己的业务需求可以参考。
if 形式
这是一种非常常见的形式,满足条件就实现 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 后面不能使用大括号 ({})。 错误指示如下:
if-else 形式
这也是一种常见的形式,满足条件显示 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 后面不能使用大括号 ({}); if 下的组件件的后面不能使用逗号(,)。 错误写法示例:
if...[widget1,widget2] 形式
该种形式也是常用于业务开发,它是当条件成立时,显示多个 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] 形式
该种形式也是常用于业务开发,它是当条件成立时,显示多个 Widget;条件不成立时,显示多个 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 下的组件集合的后面不能使用逗号(,)。 错误写法示例:
函数形式
这种形式是将这一块的逻辑抽离到另一个地方。该方法有个不足之处,那就是在不满足条件时也要返回一个 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(), ], ) // 函数形式 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. 总结
以上就是 Flutter 如何显示条件 Widget 的方式。其实还有其他的方法,例如 switch 。这些其他的方法我们在后续文章中介绍。
以上就是Flutter中显示条件Widget的实现方式的详细内容,更多关于Flutter显示Widget的资料请关注程序员之家其它相关文章!
相关文章
解决NDK开发中Eclipse报错Unresolved inclusion jni.h的最终解决方法(已测)
这篇文章主要介绍了解决NDK开发中Eclipse报错Unresolved inclusion jni.h的最终方法,需要的朋友可以参考下2016-12-12Android 中StringBuffer 和StringBuilder常用方法
这篇文章主要介绍了Android 中StringBuffer 和StringBuilder的常用方法及区别介绍,需要的朋友可以参考下2017-02-02Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
这篇文章主要介绍了Android程序开发实现QQ6.X最新版本侧滑界面效果实例代码的相关资料,需要的朋友可以参考下2016-02-02Kotlin 协程 supervisorScope {} 运行崩溃解决方法
看过很多?supervisorScope {}?文档的使用,我照抄一摸一样的代码,运行就崩溃,最后找到了解决方法,应该是kotlin版本更新做过改动,当前我使用的是?androidx.core:core-ktx:1.9.0,本文给大家介绍Kotlin 协程 supervisorScope {} 运行崩溃解决方法,感兴趣的朋友一起看看吧2024-01-01Android实现状态栏(statusbar)渐变效果的示例
本篇文章主要介绍了Android实现状态栏(statusbar)渐变效果的示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-09-09Android ListView长按弹出菜单二种实现方式示例
这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现2013-11-11
最新评论