flutter - Flutter 多状态管理
问题描述
我有一个像这样大小的盒子
child: SizedBox(
width: 50.0,
height: 50.0,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.blue
),
)
),
我想根据一个叫做_status的状态的值来改变盒子装饰的颜色,_status可以有四个值1,2,3,4,根据状态的值我想改变颜色盒子装饰像这样 1 - 蓝色 2 - 红色 3- 琥珀色 4 - 绿色
通常使用的三元语句无济于事,因为它仅适用于有限数量的状态值有没有一种方法可以实现这一点?
谢谢
解决方案
您可以定义帮助函数来计算颜色值
child: SizedBox(
width: 50.0,
height: 50.0,
child: DecoratedBox(
decoration: BoxDecoration(
color: _getBoxColor(),
),
)
),
//somewhere below build method
Color _getBoxColor() {
switch (_status) {
case 1:
return Colors.blue;
case 2:
return Colors.red;
...
}
推荐阅读
- google-apps-script - 如果日期不是 Google Apps 脚本中的今天日期,如何停止程序执行
- latex - 如何使用 biblatex 删除书目列表中的标签?
- javascript - window.matchMedia 在 Safari 中不起作用
- sql-server - 在两个日期时间之间分组
- python - Tradingview pinescript - 需要在前 2 天的高点和低点绘制水平虚线
- scala - Spark Json - 使用 nullable=false 应用架构
- pyspark - Pyspark - 固定列的汇总
- c# - 通过保存到 .txt 文件来持久化自定义类时的权限问题
- scala - Spark 转换生成未来日期
- python - Pymongo:有没有办法在获取数据时在同一个调用中使用查找和聚合