'How to correctly set Flutter Switch Widget theme
I'm trying to theme flutter's switch widget within the ThemeData constructor, and I'm having some difficulty with defining the Switch theme. When I use this for the switchTheme property:
switchTheme: SwitchThemeData(
thumbColor: MaterialStateProperty.all(primary),
),
It looks like this:
When I enter this:
switchTheme: SwitchThemeData(
thumbColor: MaterialStateProperty.all(primary),
trackColor: MaterialStateProperty.all(primary),
),
It looks like this:
What I expect/want is for the track of the switch to be grayed out when off and the thumb orange then the track and thumb to be orange when on. Is there some kind of way we're supposed to use the MaterialStateProperty class?
Solution 1:[1]
Figured it out after reading the documentation here
switchTheme: SwitchThemeData(
thumbColor: MaterialStateProperty.all(primary),
trackColor: MaterialStateProperty.resolveWith((states) =>
states.contains(MaterialState.selected) ? primary : null)),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | joshpetit |


