How to change the color of my icon button in flutter?

Question

Asked by Arefin on November 23, 2021 (source).

i am facing problem while creating an changeable icon button in flutter.I just wants it to change color while im tapping it.For creating a mark as fabourite button .Can anyone help me please?

class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}

class _p1State extends State<p1> {
@override
Widget build(BuildContext context) {
return MaterialApp(
  home: Scaffold(
   body:Stack(
       children:<Widget>[
       Image(
       image:AssetImage("Image/Chowsun1.jpg"),
     fit:BoxFit.cover,
     width: double.infinity,
     height: double.infinity,
   ),
     

      Align(
        alignment: Alignment.bottomCenter,
        child: (
              IconButton(
                icon: Icon(
                  Icons.favorite,
                    color:Colors.white


                ),
                onPressed: (){

                  Hive.box(FAVORITES_BOX);

                }
            )
        )
    )])

    ),
   );
   }
   }

Answer

Question answered by Amitha M (source).

initialise a variable say, bool isFavourite = true;

and make the following changes in the code:

                IconButton
                (
                    icon: Icon
                    (
                        Icons.favorite,
                        color: isFavourite ? Colors.red : Colors.white
                    ),
                    onPressed: ()
                    {
                        setState(() 
                        {
                            isFavourite = !isFavourite;
                        });
                    }
                )
DART FLUTTER
SHARE: