Home > Software design >  Changing Text Color onTap, Flutter
Changing Text Color onTap, Flutter

Time:10-17

I am an android developer new to flutter and i am trying to create 3 grey text widgets where when a user clicks one, the clicked one becomes blue while the others remain grey. This is the code i currently have

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,
                  ),
                ],
              ),

Also whatever i put in onTap() i get the error that says

invalid constant value

CodePudding user response:

You should create three separate variables for each button's color and then remove the const keyword in Row,

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;
        });
      },
    ),
  ],
),
  • Related