Flutter 中如何显示条件 Widget

》》》前期回顾 》》》

1. Dart 内置类型之 Record,不同于元组! 推荐指数: ⭐️⭐️⭐️⭐️⭐️

Flutter 中如何显示条件 Widget

1. 场景:

Flutter 日常开发中经常会遇见这样的需求,如: 只有用户是 VIP 时,才能展示某个入口或者某个模块。这样的需求在开发业务需求中多如牛毛,那你是如何来优雅的实现的呢?

2. 推荐实现方式

下面是本人在开发中常用的集中实现方式,博友们可以根据自己的业务需求可以参考。

1. if 形式

这是一种非常常见的形式,满足条件就实现 Widget。示例如下:

dart 复制代码
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 后面不能使用大括号 ({})。 错误指示如下:

2. if-else 形式

这也是一种常见的形式,满足条件显示 Widget1 ;不满足条件显示 Widget2 。示例如下:

dart 复制代码
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 下的组件件的后面不能使用逗号(,)。 错误写法示例:

3. if...widget1,widget2 形式

该种形式也是常用于业务开发,它是当条件成立时,显示多个 Widget。 示例如下:

dart 复制代码
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,
      )
    ],
  ],
)
4. if...widget1,widget2 else...widget3,widget4 形式

该种形式也是常用于业务开发,它是当条件成立时,显示多个 Widget;条件不成立时,显示多个 Widget。 示例如下:

dart 复制代码
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 下的组件集合的后面不能使用逗号(,)。 错误写法示例:

5. 函数形式

这种形式是将这一块的逻辑抽离到另一个地方。该方法有个不足之处,那就是在不满足条件时也要返回一个 Widget。 示例如下:

dart 复制代码
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 。这些其他的方法我们在后续文章中介绍。

相关推荐
ZacJi30 分钟前
当苹果将系统 API 划归 @MainActor:一次跨 SDK 版本的 Swift 并发踩坑与破局实战
ios·swift
mardelan1 小时前
2026年iOS短视频总结工具测评强识别提效率 整理清晰更省心
ios·音视频
huibin1478523693 小时前
热缓解学习记录
android·学习
ZZH_AI项目交付4 小时前
638 处旧名称残留——Swift/ObjC 混编项目安全重命名完整指南
ios·objective-c·swift
ZZH_AI项目交付4 小时前
Apple Silicon 模拟器遇到旧版 MLKit:一次完整的依赖排查
ios·app·编译器
kango4 小时前
iOS 项目工程化构成与 Xcode 架构详解
ios·程序员
Fungleo1069974 小时前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
flutter
小田的博客5 小时前
SAP MM 供应商银行主数据更新报错!message R1228!
android·java·服务器
2501_915918416 小时前
iOS 怎么抓包?抓包鹰系统级 网卡 应用层三种方式对比,不越狱抓 iPhone 流量
网络协议·计算机网络·网络安全·ios·adb·https·udp
GitLqr6 小时前
Flutter 实战:为你的 App 增加桌面快捷方式 (Quick Actions)
flutter·app·全栈