Skip to content

Instantly share code, notes, and snippets.

@hillelcoren
Created January 11, 2019 11:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hillelcoren/d3c5e27b9e52f9f8560f87ca67cbf751 to your computer and use it in GitHub Desktop.
Save hillelcoren/d3c5e27b9e52f9f8560f87ca67cbf751 to your computer and use it in GitHub Desktop.
Enable/disable dark mode with Redux
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
void main() {
final store =
Store<AppState>(reducer, initialState: AppState(enableDarkMode: false));
runApp(MyApp(store: store));
}
class MyApp extends StatefulWidget {
const MyApp({Key key, this.store}) : super(key: key);
final Store<AppState> store;
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
final store = widget.store;
return StoreProvider<AppState>(
store: store,
child: AppBuilder(
builder: (BuildContext context) {
return MaterialApp(
theme: ThemeData(
brightness: store.state.enableDarkMode
? Brightness.dark
: Brightness.light),
home: Scaffold(
appBar: AppBar(
title: Text('Test Redux App'),
),
body: SettingsView(),
),
);
},
),
);
}
}
class SettingsView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreBuilder(builder: (BuildContext context, Store<AppState> store) {
final state = store.state;
return SwitchListTile(
title: Text('Dark Mode'),
value: state.enableDarkMode,
onChanged: (value) {
store.dispatch(UpdateDarkMode(enable: !state.enableDarkMode));
AppBuilder.of(context).rebuild();
},
secondary: Icon(Icons.settings),
);
});
}
}
// Redux Claaes
class AppState {
AppState({this.enableDarkMode});
bool enableDarkMode;
}
class UpdateDarkMode {
UpdateDarkMode({this.enable});
final bool enable;
}
AppState reducer(AppState state, dynamic action) {
if (action is UpdateDarkMode) {
return AppState(
enableDarkMode: action.enable,
);
}
return state;
}
// App Builder
class AppBuilder extends StatefulWidget {
const AppBuilder({Key key, this.builder}) : super(key: key);
final Function(BuildContext) builder;
@override
AppBuilderState createState() => new AppBuilderState();
static AppBuilderState of(BuildContext context) {
return context.ancestorStateOfType(const TypeMatcher<AppBuilderState>());
}
}
class AppBuilderState extends State<AppBuilder> {
@override
Widget build(BuildContext context) {
return widget.builder(context);
}
void rebuild() {
setState(() {});
}
}
@yanshiyason
Copy link

❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️

@om-ha
Copy link

om-ha commented Sep 13, 2022

Thanks for this @hillelcoren

Note for others:

This will not rebuild constant widgets. For that you need one of these:

  • A. Depend on a central inherited widget within constant widgets you want to rebuild. See here.
  • B. Restart the app on flutter level not OS level. See here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment