【问题标题】:Flutter: control multiple iconsFlutter:控制多个图标
【发布时间】:2021-05-31 09:53:45
【问题描述】:

我想让用户选择一个图标,他们应该只能选择一个。所选图标更改其颜色,而所有其他图标保持不变。我该怎么做?

代码:

            IconButton(
              iconSize: 25,
              onPressed: () {
                setState(() {
                  isPressed = !isPressed;
                });
              },
              icon: Icon(
                ESGIcons.star,
              ),
              color: (isPressed)
                  ? AppTheme.esgEasyYellow
                  : AppTheme.esgDarkBlue,
            ),
            IconButton(
              iconSize: 25,
              onPressed: () {
                setState(() {
                  isPressed = !isPressed;
                });
              },
              icon: Icon(
                ESGIcons.crown,
              ),
              color: (isPressed)
                  ? AppTheme.esgEasyYellow
                  : AppTheme.esgDarkBlue,
            ),

【问题讨论】:

标签: flutter dart


【解决方案1】:

试试这个

List<Icon> icons = [
  ESGIcons.star,
  ESGIcons.crown,
];
int buttonOnPressed = -1;
ListView.builder(
    scrollDirection : Axis.horizontal,
    shrinkWrap: true,
    itemCount: icons.length,
    itemBuilder: (BuildContext context, int index) {
      return IconButton(
        iconSize: 25,
        onPressed: () {
          setState(() {
            buttonOnPressed = index;
          });
        },
        icon: Icon(icons[index]),
        color: (buttonOnPressed == index)
            ? AppTheme.esgEasyYellow
            : AppTheme.esgDarkBlue,
      );
    });

【讨论】:

  • 谢谢。这只需一个小编辑:icon: Icon(icons[index]), 你知道如何将图标放在一行而不是一列或列表中吗?
  • 我更新可能会回答,你的意思是把它放在水平方式?你可以试试加scrollDirection : Axis.horizontal,
猜你喜欢
  • 2020-01-19
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多