Search code examples
flutterbuttondart

Change Icon of toggle button flutter


Is there anyway to change icon of the toggle button on pressed or Is there anyway to make icon button function like toggle button?

I've tried following method but I want to change Icon agian to first one if the button pressed agian.

IconButton( iconSize: 30.0,
            padding: EdgeInsets.only(left:4,right:4,top:0),
            icon: Padding(
            child: pressed ==true ? 
            Icon(Icons.start):Icon(Icons.stop)
                  ),
                  onPressed: () {
                    setState(() {
                      pressed=true;
                    });
                  }

Solution

  • IconButton(
            iconSize: 30.0,
            padding: EdgeInsets.only(left: 4, right: 4, top: 0),
            icon: Padding(
                padding: EdgeInsets.all(8.0),
                child: pressed == true ? Icon(Icons.start) : Icon(Icons.stop)),
            onPressed: () {
              setState(() {
                pressed = !pressed;
              });
            },
          ),
    

    pressed = !pressed - Is the same as saying pressed is equal to what pressed is currently not. So on press it will switch to true if boolean is false, and false if boolean is true.