【问题标题】:flutter multiple state managementFlutter 多状态管理
【发布时间】:2021-04-11 12:29:35
【问题描述】:

我有一个像这样大小的盒子

    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 - 绿色

通常使用的三元语句没有帮助,因为它只适用于有限数量的状态值,有没有办法可以实现这个?

谢谢

【问题讨论】:

    标签: flutter flutter-state


    【解决方案1】:

    您可以定义辅助函数来计算颜色值

    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;
       ...
     }
    

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2021-03-04
      • 2019-11-23
      • 2021-05-03
      • 2019-06-19
      • 1970-01-01
      • 2020-12-29
      • 2019-08-04
      • 2023-02-14
      相关资源
      最近更新 更多