【问题标题】:Changing Text Color onTap, Flutter改变文本颜色 onTap, Flutter
【发布时间】:2021-12-04 10:47:05
【问题描述】:

我是一名刚接触 Flutter 的 android 开发人员,我正在尝试创建 3 个灰色文本小部件,当用户单击一个时,单击的一个变为蓝色,而其他的保持灰色。这是我目前拥有的代码

Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: const <Widget>[

                  InkWell(
                    child: Text('Click 3',
                        style: TextStyle(
                          color: Colors.indigo,
                          fontSize: 15.0,
                          fontWeight: FontWeight.bold,
                        )),
                    onTap: () => Colors.blue,
                  ),




                  InkWell(
                    child: Text('Click 2',
                        style: TextStyle(
                          color: Colors.indigo,
                          fontSize: 15.0,
                          fontWeight: FontWeight.bold,
                        )),
                    onTap: () => Colors.blue,
                  ),


                  InkWell(
                    child: Text('Click 3',
                        style: TextStyle(
                          color: Colors.indigo,
                          fontSize: 15.0,
                          fontWeight: FontWeight.bold,
                        )),
                    onTap: () => Colors.blue,
                  ),
                ],
              ),

无论我在 onTap() 中输入什么,我都会收到错误提示

invalid constant value

【问题讨论】:

    标签: ios flutter dart mobile-development


    【解决方案1】:

    您应该为每个按钮的颜色创建三个单独的变量,然后删除 Row 中的 const 关键字,

    Color firstColor = Colors.indigo;
    Color secondColor = Colors.indigo;
    Color thirdColor = Colors.indigo;
    ...
    Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[  /// remove const keyword
        InkWell(
          child: Text('Click 3',
            style: TextStyle(
              color: firstColor,
              fontSize: 15.0,
              fontWeight: FontWeight.bold,
            ),
          ),
          onTap: (){
            setState((){
              firstColor=Colors.blue;
            });
          },
        ),
        InkWell(
          child: Text('Click 2',
            style: TextStyle(
              color: secondColor,
              fontSize: 15.0,
              fontWeight: FontWeight.bold,
            ),
          ),
          onTap: (){
            setState((){
              secondColor=Colors.blue;
            });
          },
        ),
        InkWell(
          child: Text('Click 3',
            style: TextStyle(
            color: thirdButton,
            fontSize: 15.0,
            fontWeight: FontWeight.bold,
            ),
          ),
          onTap: (){
            setState((){
              thirdColor=Colors.blue;
            });
          },
        ),
      ],
    ),
    

    【讨论】:

    • 非常感谢,您的解释和示例非常清楚,我不仅实现了我想要的功能,而且还实现了其他类似的功能
    猜你喜欢
    • 2021-11-13
    • 2021-05-15
    • 1970-01-01
    • 2019-09-30
    • 2021-08-22
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    相关资源
    最近更新 更多