button icon onpressed in flutter code example
Example 1: flutter change the icon of icon button on pressed
bool selected = true;
//...
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: IconButton(
icon: Icon( selected ? Icons.celebration : Icons.title),
onPressed: () {
setState(() {
selected = !selected;
});
},
), //IconButton
), //Center
); //Scaffold
}
Example 2: icon onpressed flutter
IconButton( icon: Icon(Icons.bluetooth), color: Colors.grey, highlightColor: Colors.red, hoverColor: Colors.green, focusColor: Colors.purple, splashColor: Colors.yellow, disabledColor: Colors.amber, iconSize: 48, onPressed: () { setState(() { _isBluetoothOn = !_isBluetoothOn; }); }, ),