【问题标题】:flutter chips and colors - Why is ChoiceChip so different颤动的芯片和颜色 - 为什么 ChoiceChip 如此不同
【发布时间】:2019-08-18 21:28:10
【问题描述】:

假设以下代码

ChipTheme(
                data: theme.chipTheme.copyWith(
                  //labelStyle: TextStyle(color: Colors.black),
                  secondaryLabelStyle: TextStyle(color: Colors.black),
                  //selectedColor: Colors.black,
                  secondarySelectedColor: Colors.grey[300],
                  //brightness: Brightness.light
                ),
                child:
                  Row(
                    children: <Widget>[
                      Chip(
                        //selected: currentLevel >= 1,
                        label: new Text("Level 1"),
                        avatar: new CircleAvatar(
                          backgroundColor: Colors.green,
                          child: Text("1")
                        ),
                      ),
                      ChoiceChip(
                        selected: currentLevel >= 2,
                        label: new Text("Level 2"),
                        avatar: new CircleAvatar(
                            backgroundColor: Colors.green,
                            child: Text("2")
                        ),
                      ),
                      ChoiceChip(
                        selected: currentLevel >= 3,
                        label: new Text("Level 3"),
                        avatar: new CircleAvatar(
                            backgroundColor: Colors.green,
                            child: Text("3")
                        ),
                      ),
                    ],
                  ),
              ),

此结果如下所示:

第一个是一个简单的芯片小部件。这就是我想要的“选择”ChoiceChip。但是正如你所看到的,第二个芯片与第一个相比看起来不同。这是为什么?我对第三个未选择的 ChoiceChip 感到满意。无论我做什么,我都无法让第二个看起来像第一个,因为 ChoiceChip 似乎使所有东西都变暗(绿色头像和背景颜色)。

所选的 ChoiceChip 甚至看起来不像已选中或处于活动状态。它看起来几乎和未选中的一样(第三个)。

感谢您提供如何正确设置样式的任何提示。

【问题讨论】:

  • 你要单选还是多选?

标签: flutter material-design


【解决方案1】:

看,解决方案可能会让您感到沮丧。至少在我弄清楚之前,这让我很沮丧。

ChoceChip 的行为类似于按钮。现在按钮需要一个非空的 onPressed 参数。如果未提供,则呈现按钮的禁用状态。

同样,ChoiceChip 需要一个非空的 onSelected 参数。所以一个有效的 ChoiceChip 声明如下所示:

ChoiceChip(
    selected: currentLevel >= 2,
    label: new Text("Level 2"),
    avatar: new CircleAvatar(
        backgroundColor: Colors.green,
        child: Text("2")
    ),
    onSelected: (value) {}
),

瞧,你有漂亮的 ChoiceChips。

【讨论】:

    猜你喜欢
    • 2022-10-18
    • 1970-01-01
    • 2016-09-18
    • 2020-10-20
    • 2023-04-08
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多