Created
January 26, 2021 18:01
-
-
Save littleironical/0961eead03463c6264784fcdcf906269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
final _notifier = ValueNotifier<ThemeModel>(ThemeModel(ThemeMode.light)); | |
@override | |
Widget build(BuildContext context) { | |
return ValueListenableBuilder<ThemeModel>( | |
valueListenable: _notifier, | |
builder: (_, model, __) { | |
final mode = model.mode; | |
return MaterialApp( | |
theme: ThemeData.light(), // Provide light theme. | |
darkTheme: ThemeData.dark(), // Provide dark theme. | |
themeMode: mode, // Decides which theme to show. | |
home: Scaffold( | |
appBar: AppBar(title: Text('Light/Dark Theme')), | |
body: RaisedButton( | |
onPressed: () => _notifier.value = ThemeModel(mode == ThemeMode.light ? ThemeMode.dark : ThemeMode.light), | |
child: Text('Toggle Theme'), | |
), | |
), | |
); | |
}, | |
); | |
} | |
} | |
class ThemeModel with ChangeNotifier { | |
final ThemeMode _mode; | |
ThemeMode get mode => _mode; | |
ThemeModel(this._mode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment