【问题标题】:Flutter conditional statement with Card(color:)使用 Card(color:) Flutter 条件语句
【发布时间】:2019-03-11 11:36:11
【问题描述】:

如果变量的值大于 10,我正在尝试将下面的颜色从红色更改为绿色。我该怎么做? as 'color:' 不接受 if, else 语句:

         Card( 
            child: Column(
              children:[
                Text('Calls Taken',
                style: TextStyle(
                  fontSize: 16.0,
                  decoration: TextDecoration.underline,
                ),),
                Text('10'),
              ],
            ),
            color: Colors.redAccent

          ),

上面的卡片小部件在继承StatelfulWidgets的类下

提前谢谢你!!!

【问题讨论】:

  • 您是否尝试将逻辑导出到返回颜色类型的函数?

标签: flutter flutter-layout


【解决方案1】:

只要使用三元 if

color: variable > 10 ? Colors.redAccent : Colors.green

【讨论】:

  • 很高兴听到:)
  • 你能在多个条件下做到这一点吗?即一个 else if 语句
  • @12rhombiingridwnocorners color: (variable1 > 10 && s == "foo") ? Colors.redAccent : Colors.green。如果您想要更复杂的 if/else 组合,您仍然可以这样做。您甚至可以嵌套这些 if/else,但它们变得难以阅读。在这种情况下,我强烈建议将代码移到方法中。
猜你喜欢
  • 2020-09-19
  • 2022-01-15
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多