Update Theme Setup (#2300)

* refactor(theme-setup-update): Move didChangePlatformBrightness lifecycle method to Root widget instead of AppState. We already have an active WidgetsBindingObserver in Root widget. * fix(theme): Add workaround for flutter iOS didChangePlatformBrightness issue causing unneeded rebuilds.

David Adegoke committed May 29, 2025 at 14:46 UTC b77c22b0dfff37e8fb5086791dc0dedca5dad5de
3 files changed +15 -26
lib/main.dart
+1 -25
@@ -25,7 +25,6 @@ import 'package:cake_wallet/routes.dart';
25 import 'package:cake_wallet/src/screens/root/root.dart';
26 import 'package:cake_wallet/store/app_store.dart';
27 import 'package:cake_wallet/store/authentication_store.dart';
28 -import 'package:cake_wallet/themes/core/material_base_theme.dart';
28 import 'package:cake_wallet/themes/utils/theme_provider.dart';
29 import 'package:cake_wallet/utils/device_info.dart';
30 import 'package:cake_wallet/utils/exception_handler.dart';
@@ -290,30 +289,7 @@ class App extends StatefulWidget {
289 AppState createState() => AppState();
290 }
291
293 -class AppState extends State<App> with SingleTickerProviderStateMixin, WidgetsBindingObserver {
294 - @override
295 - void initState() {
296 - super.initState();
297 - WidgetsBinding.instance.addObserver(this);
298 - }
299 -
300 - @override
301 - void dispose() {
302 - WidgetsBinding.instance.removeObserver(this);
303 - super.dispose();
304 - }
305 -
306 - @override
307 - void didChangePlatformBrightness() {
308 - final appStore = getIt.get<AppStore>();
309 - if (appStore.themeStore.themeMode == ThemeMode.system) {
310 - final systemTheme = appStore.themeStore.getThemeFromSystem();
311 - if (appStore.themeStore.currentTheme != systemTheme) {
312 - appStore.themeStore.setTheme(systemTheme);
313 - }
314 - }
315 - }
316 -
292 +class AppState extends State<App> with SingleTickerProviderStateMixin {
293 @override
294 Widget build(BuildContext context) {
295 return Observer(
lib/src/screens/root/root.dart
+13
@@ -1,4 +1,5 @@
1 import 'dart:async';
2 +import 'dart:io';
3 import 'package:cake_wallet/bitcoin/bitcoin.dart';
4 import 'package:cake_wallet/core/auth_service.dart';
5 import 'package:cake_wallet/core/totp_request_details.dart';
@@ -159,6 +160,18 @@ class RootState extends State<Root> with WidgetsBindingObserver {
160 }
161 }
162
163 + @override
164 + void didChangePlatformBrightness() {
165 + if (widget.appStore.themeStore.themeMode == ThemeMode.system) {
166 + Future.delayed(Duration(milliseconds: Platform.isIOS ? 500 : 0), () {
167 + final systemTheme = widget.appStore.themeStore.getThemeFromSystem();
168 + if (widget.appStore.themeStore.currentTheme != systemTheme) {
169 + widget.appStore.themeStore.setTheme(systemTheme);
170 + }
171 + });
172 + }
173 + }
174 +
175 @override
176 Widget build(BuildContext context) {
177 // this only happens when the app has been in the background for some time
lib/themes/core/theme_store.dart
+1 -1
@@ -168,7 +168,7 @@ abstract class ThemeStoreBase with Store {
168 }
169
170 MaterialThemeBase getThemeFromSystem() {
171 - final systemBrightness = WidgetsBinding.instance.window.platformBrightness;
171 + final systemBrightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
172 return systemBrightness == Brightness.dark ? ThemeList.darkTheme : ThemeList.lightTheme;
173 }
174 }