How do I change the color of the icon when pressing the button so that it remains in its new color and does not change after the second press or exiting the application.
You can do it in several ways: Using StatefulWidget, Bloc, Provider, ValueListenable, etc. The most simple way is a StatefulWidget. You have to save the current state of the button on a variable on the widget state, and you call the method setState() on the onPressed parameter on the Button widget. So, on the Icon widget, you choose the color based on the button state.
The widget state should looks something like this:
3
Answers
Save state of button in local data and use this data when create UI for color of button. There are ways to save and read data in Flutter,
There are a few options:
SharedPreferences via a Flutter plugin: shared_preferences
Localstore via a Flutter plugin: localstore
Hive via a Flutter plugin: hive
You can do it in several ways: Using StatefulWidget, Bloc, Provider, ValueListenable, etc. The most simple way is a StatefulWidget. You have to save the current state of the button on a variable on the widget state, and you call the method setState() on the onPressed parameter on the Button widget. So, on the Icon widget, you choose the color based on the button state.
The widget state should looks something like this:
But, remember, my friend. Try to use logic as little as posible on the user interface. Just little things like colors and so on.
Use ValueNotifier for notifying color of the button.
inside code