简体   繁体   中英

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

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);

                }
            )
        )
    )])

    ),
   );
   }
   }

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;
                        });
                    }
                )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM