I need to create ElevatedButton that uses SvgPicture for displaying icon inside of it. I have defined foregroundColor inside ButtonStyle to resolve color in accordance with current MaterialStates. However, this foregroundColor is being applied only to Text and Icon widgets. How can I resolve this color the same way for my SvgPicture widget? Or atleast have an access to set of MaterialState of my ElevatedButton?
I tried to research the way Flutter stores set of MaterialState for ElevatedButton, but still haven’t found out it yet. Also, I came across MaterialStateMixin and as I understand its purpose is solely dedicated to create custom buttons in Flutter with the help of GestureDetector, InkWell, etc.
2
Answers
To access the set of MaterialState in Flutter, you can use the MaterialStateColor class. However, it’s important to note that SvgPicture may not automatically respond to changes in MaterialState like Icon does. In such cases, you may need to manually manage the color changes for your SvgPicture.
Here’s an example of how you can achieve this:
In this example, both the foregroundColor of the ElevatedButton and the color of the SvgPicture are manually managed based on the MaterialState. Adjust the colors and conditions as needed for your specific use case. Keep in mind that SVG rendering might not fully support dynamic color changes based on MaterialState, and you may need to use alternative approaches or packages for more complex scenarios.
I believe what you are looking for is
MaterialStatesController
which exposes the widget’s material statesSet<MaterialState>
.https://api.flutter.dev/flutter/material/MaterialStatesController-class.html