【问题标题】:Flutter-Issue with Conditional Color within Card Widget卡片小部件中带有条件颜色的颤振问题
【发布时间】:2020-05-18 02:31:54
【问题描述】:

我是 Flutter 的新手,并试图根据多个字符串条件(例如“someText”)更改我正在创建的 SliverList() 的 Card() 颜色。目前,我有 Card() 根据单个条件更改颜色,但我无法弄清楚如何将多个条件应用于此参数。我想为颜色提供多个字符串条件,如果不满足条件,则具有不同的颜色以及默认颜色。任何帮助将不胜感激。

 SliverList(
            delegate: SliverChildBuilderDelegate(
                    (context,index) => Card(
                      color: items[index].theTopic =='someText' ? Colors.greenAccent : Colors.yellow,

                      child: InkWell(
                        onTap: () => _navigateToNote(context, items[index]),
                        customBorder: cardShape,),
              childCount: items.length
            ),
          )

【问题讨论】:

    标签: flutter colors widget conditional-statements


    【解决方案1】:

    您可以在 else 部分连续添加条件,无论您需要多少条件。

    例如:

    color: items[index].theTopic =='someText' ? Colors.greenAccent :  items[index].theTopic =='otherText' ? Colors.amber : items[index].theTopic =='anotherText' ? Colors.red : Colors.black, // last black color will become default color
    

    【讨论】:

    • @m 不欢迎您!接受并支持我对你的回答发现它很有用。
    猜你喜欢
    • 2020-04-12
    • 2019-12-04
    • 2020-11-08
    • 2018-11-12
    • 2021-07-19
    • 1970-01-01
    • 2019-12-20
    • 2019-08-14
    • 2020-04-13
    相关资源
    最近更新 更多