diff --git a/lib/main.dart b/lib/main.dart index f74bdc4e4..9f8d7b24c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -32,8 +32,11 @@ import 'package:invoiceninja_flutter/ui/invoice/invoice_screen.dart'; import 'package:invoiceninja_flutter/utils/localization.dart'; void main() async { + final prefs = await SharedPreferences.getInstance(); + final enableDarkMode = prefs.getBool(kSharedPrefEnableDarkMode); + final store = Store(appReducer, - initialState: AppState(), + initialState: AppState(enableDarkMode: enableDarkMode), middleware: [] ..addAll(createStoreAuthMiddleware()) ..addAll(createStoreDashboardMiddleware()) @@ -45,16 +48,12 @@ void main() async { LoggingMiddleware.printer(), ])); - final prefs = await SharedPreferences.getInstance(); - final enableDarkMode = prefs.getBool(kSharedPrefEnableDarkMode); - - runApp(InvoiceNinjaApp(store: store, enableDarkMode: enableDarkMode)); + runApp(InvoiceNinjaApp(store: store)); } class InvoiceNinjaApp extends StatefulWidget { final Store store; - final bool enableDarkMode; - const InvoiceNinjaApp({Key key, this.store, this.enableDarkMode}) + const InvoiceNinjaApp({Key key, this.store}) : super(key: key); @override @@ -75,7 +74,7 @@ class InvoiceNinjaAppState extends State { ], // light theme - theme: widget.enableDarkMode + theme: widget.store.state.uiState.enableDarkMode ? ThemeData( brightness: Brightness.dark, accentColor: Colors.lightBlueAccent, diff --git a/lib/redux/app/app_reducer.dart b/lib/redux/app/app_reducer.dart index c9ee894bc..0c20d2187 100644 --- a/lib/redux/app/app_reducer.dart +++ b/lib/redux/app/app_reducer.dart @@ -9,7 +9,9 @@ import 'package:invoiceninja_flutter/redux/company/company_reducer.dart'; // We create the State reducer by combining many smaller reducers into one! AppState appReducer(AppState state, dynamic action) { if (action is UserLogout) { - return AppState().rebuild((b) => b.authState.replace(state.authState)); + return AppState().rebuild((b) => b + ..authState.replace(state.authState) + ..uiState.enableDarkMode = state.uiState.enableDarkMode); } else if (action is LoadStateSuccess) { return action.state.rebuild((b) => b ..isLoading = false diff --git a/lib/redux/app/app_state.dart b/lib/redux/app/app_state.dart index 211f8c779..d7956f48b 100644 --- a/lib/redux/app/app_state.dart +++ b/lib/redux/app/app_state.dart @@ -16,7 +16,7 @@ part 'app_state.g.dart'; abstract class AppState implements Built { - factory AppState() { + factory AppState({bool enableDarkMode}) { return _$AppState._( isLoading: false, isSaving: false, @@ -27,7 +27,7 @@ abstract class AppState implements Built { companyState3: CompanyState(), companyState4: CompanyState(), companyState5: CompanyState(), - uiState: UIState(), + uiState: UIState(enableDarkMode: enableDarkMode), ); } AppState._(); diff --git a/lib/redux/ui/ui_state.dart b/lib/redux/ui/ui_state.dart index 1bb836744..91a836155 100644 --- a/lib/redux/ui/ui_state.dart +++ b/lib/redux/ui/ui_state.dart @@ -9,11 +9,11 @@ part 'ui_state.g.dart'; abstract class UIState implements Built { - factory UIState() { + factory UIState({bool enableDarkMode}) { return _$UIState._( selectedCompanyIndex: 0, currentRoute: LoginScreen.route, - enableDarkMode: false, + enableDarkMode: enableDarkMode ?? false, productUIState: ProductUIState(), clientUIState: ClientUIState(), invoiceUIState: InvoiceUIState(),