颤振多状态管理

flutter multiple state management

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

    child: SizedBox(
        width: 50.0,
        height: 50.0,
        child: DecoratedBox(
          decoration: BoxDecoration(
              color: Colors.blue
          ),
        )
    ),

我想根据状态块_status的值改变盒子装饰的颜色,_status可以有四个值1,2,3,4,根据status的值我会喜欢像这样改变盒子装饰的颜色 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;
   ...
 }