Arun Praseeth
Arun Praseeth

Reputation: 13

how to change iconbutton colour immediately on pressed (flutter)

This code works fine with only one iconbutton. But when I create multiple iconbuttons, the scenario is different. With three(3) icon buttons, on pressing anyone(1) icon, all the three(3) icons changes it's colour. How to change colour individually?

class _userProfileScreenState extends State<userProfileScreen> {
  Color _iconcolor = Colors.black;


IconButton(
      onPressed: () {
      setState(() {
          _iconcolor = Color(0xff187bcd);
           },);
      },
      icon: Icon(
          FontAwesomeIcons.mars,
          color: _iconcolor, //male
          size: 45,
          ),
      ),

Upvotes: 0

Views: 1212

Answers (2)

Sayanc2000
Sayanc2000

Reputation: 222

This is simple, try creating 3 variables which hold color value for 3 buttons

class SomeState extends State<StatefulWidget> {
  Color _iconColor1 = Colors.white;
  Color _iconColor2 = Colors.black;
  Color _iconColor3 = Colors.green
  @override
  Widget build(BuildContext) {
    return Row(
        children:[
        IconButton(
            icon: Icon(Icons.share, color: _iconColor1,),
            
            onPressed: () {
                 setState((){
                      _iconColor1= //setNew Color
                 });
           }     
        ),
        //enter 2 more icons like this and change color to different variable.
       ]
    )
}
}

Upvotes: 0

Janvi Patel
Janvi Patel

Reputation: 252

You could do something like this

class SomeState extends State<StatefulWidget> {
  Color _iconColor = Colors.white;

  @override
  Widget build(BuildContext) {
    return ListTile(
      leading: new IconButton(
        icon: Icon(Icons.star, color: _iconColor),
        onPressed: () {
          setState(() {
          _iconColor = Colors.yellow;
        });
      },
    );
  }
}

Upvotes: 1

Related Questions