OnPressed 将 activeColor 设置为列表的按钮,并将 inactiveColor 设置为其他 btns - Flutter

OnPressed Set activeColor to a button of a list, and set inactiveColor to others btns - Flutter

我有一个筹码列表,我希望它们在用户单击时改变颜色。

例如,如果我点击第一个筹码,它的颜色会变成黑色,而其他所有筹码都是灰色的。然后,如果我点击第二个芯片,它的颜色变为黑色,第一个芯片颜色变为灰色,依此类推。

我找不到 beautiful/simple 的方法,你有什么想法吗?

非常感谢

以下是您的操作方法:

  Widget _myChip(int number, String name) {
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new InkWell(
        child: new Chip(
            label: new Text(name,
            style: new TextStyle(
              color: selectedChip == number ? Colors.white : Colors.black
            ),),
            backgroundColor:
                selectedChip == number ? Colors.black : Colors.grey),
        onTap: () {
          setState(() {
            selectedChip = number;
          });
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Whosebug'),
      ),
      body: new Column(
        children: <Widget>[
          _myChip(1, 'Arnold'),
          _myChip(2, 'Sylvester'),
          _myChip(3, 'Priscilla'),
          _myChip(4, 'Parge'),
          _myChip(5, 'Something'),
        ],
      ),
    );
  }

你需要给筹码一个唯一的编号来识别和使用内联如果改变筹码的颜色。