The ThemeSwitcher widget is the primary entry point for implementing animated theme changes. It provides a context in which you can trigger a transition between themes using a specified clipper (which defines the shape of the animation).
There are three ways to instantiate ThemeSwitcher:
- Standard Constructor: Requires a
builder function that receives only BuildContext. ThemeSwitcher.switcher factory: A convenience constructor that automatically provides the ThemeSwitcherState to your builder via ThemeSwitcher.of(context).ThemeSwitcher.withTheme factory: A convenience constructor that provides both the ThemeSwitcherState and the current ThemeData to your builder.
To trigger the animation, call changeTheme on the ThemeSwitcherState instance.
// Using the .switcher factory for easy access to the state
ThemeSwitcher.switcher(
clipper: const ThemeSwitcherCircleClipper(),
builder: (context, switcher) {
return IconButton(
icon: Icon(Icons.brightness_6),
onPressed: () {
// Trigger the animated transition
switcher.changeTheme(theme: ThemeData.dark());
},
);
},
);