2

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

                }
            )
        )
    )])

    ),
   );
   }
   }

1 Answer 1

3

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;
                        });
                    }
                )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.