0

Here's what I have...

IconButton(icon: Icon (Icons.bookmark_outline, color: Color(0xFF192A4F),),
                                      onPressed: () => Icons.bookmark, color: Color(0xFF192A4F),),

I don't know what I'm doing. All I've been able to find is this

how to fill color of IconButton in Flutter

but I couldn't get that to work.

I just want the icon to change when tapped... I'm new.

What worked:

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool outline = false;
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
Row(
    children: <Widget>[

      IconButton(icon: Icon(outline ? Icons.bookmark_outline : Icons.bookmark),
          onPressed:(){
            setState((){
              outline = !outline;
            });
          }
      ),
      SizedBox(width: 8.0),
      Text('Bookmark', style: TextStyle(color: Color(0xFF192A4F)),),
    ],
  ),
Leena Marie
  • 187
  • 3
  • 22

1 Answers1

0

you need do like this,

create a boolean variable,

bool outline = false;

now in the on press function,

onPressed:(){
setState(){
  outline = !outline;
}
}

now for the your Icon do something like this,

icon: Icon(outline ? Icons.bookmark_outline : Icons.bookmark):

Make sure your are using stateful widget

Pokaboom
  • 1,110
  • 9
  • 28
  • Thank you for your answer, unfortunately it's not working for me... it's stuck on Icons.bookmark. It only changes to Icons.bookmark_outline if I change bool outline to true... I don't understand – Leena Marie May 24 '21 at 18:45
  • @Leena Marie Can you update the question with the code – Pokaboom May 24 '21 at 18:59
  • @LeenaMarie add the boolean variable inside the state class, like this `class _HomeState extends State {bool outline = false; @override ...}` – Pokaboom May 25 '21 at 11:25
  • It's still not working... I updated my code – Leena Marie May 25 '21 at 11:41
  • @LeenaMarie Hey! there might be something that you are doing wrong so try this answer https://stackoverflow.com/a/49125284/13109948 it has same functionality as your it toggles password's visibility so this might help – Pokaboom May 25 '21 at 12:17
  • Thank you! It's changing now :) I missed a bracket... – Leena Marie May 25 '21 at 14:28