From fccb79b2cc1ab58328fcd475a0772eddb5fce2be Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 16 Jan 2020 12:45:04 +0800 Subject: [PATCH] fix: follow system brightness --- lib/app.dart | 7 +------ lib/home.dart | 1 - lib/models/theme.dart | 32 +++++++++++++++----------------- lib/scaffolds/common.dart | 3 ++- lib/screens/object.dart | 2 +- lib/screens/user.dart | 2 +- lib/utils/utils.dart | 6 ++++-- 7 files changed, 24 insertions(+), 29 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index 4b1dd69..c532bf3 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -16,12 +16,7 @@ class MyApp extends StatelessWidget { ); default: return MaterialApp( - theme: ThemeData( - brightness: theme.brightness, - // primaryColorBrightness: theme.brightness, - primaryColorLight: theme.paletteLight.background, - primaryColorDark: theme.paletteDark.background, - ), + theme: ThemeData(brightness: theme.brightness), home: Home(), ); } diff --git a/lib/home.dart b/lib/home.dart index 7d37568..54b30be 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -137,7 +137,6 @@ class _HomeState extends State { }); }, ); - default: return Scaffold( body: _buildScreen(active), diff --git a/lib/models/theme.dart b/lib/models/theme.dart index e0767a2..05993d6 100644 --- a/lib/models/theme.dart +++ b/lib/models/theme.dart @@ -97,28 +97,26 @@ class Palette { } class ThemeModel with ChangeNotifier { - static const kTheme = 'theme'; static const kBrightness = 'brightness'; int _theme; int get theme => _theme; bool get ready => _theme != null; - int _brightnessValue = AppBrightnessType.followSystem; - int get brighnessValue => _brightnessValue; - - /// not null - Brightness brightnessOf(BuildContext context) { - switch (_brightnessValue) { - case AppBrightnessType.light: - return Brightness.light; - case AppBrightnessType.dark: - return Brightness.dark; - default: - return MediaQuery.of(context).platformBrightness; + Brightness systemBrightness = Brightness.light; + void setSystemBrightness(Brightness v) { + // print('systemBrightness: $v'); + if (v != systemBrightness) { + Future.microtask(() { + systemBrightness = v; + notifyListeners(); + }); } } + int _brightnessValue = AppBrightnessType.followSystem; + int get brighnessValue => _brightnessValue; + // could be null Brightness get brightness { switch (_brightnessValue) { @@ -127,7 +125,7 @@ class ThemeModel with ChangeNotifier { case AppBrightnessType.dark: return Brightness.dark; default: - return null; + return systemBrightness; } } @@ -161,7 +159,7 @@ class ThemeModel with ChangeNotifier { ); Palette paletteOf(BuildContext context) { - switch (brightnessOf(context)) { + switch (brightness) { case Brightness.light: return paletteLight; case Brightness.dark: @@ -173,7 +171,7 @@ class ThemeModel with ChangeNotifier { Future init() async { final prefs = await SharedPreferences.getInstance(); - final v = prefs.getInt(kTheme); + final v = prefs.getInt(StorageKeys.theme); Fimber.d('read theme: $v'); if (AppThemeType.values.contains(v)) { _theme = v; @@ -194,7 +192,7 @@ class ThemeModel with ChangeNotifier { Future setTheme(int v) async { _theme = v; final prefs = await SharedPreferences.getInstance(); - await prefs.setInt(kTheme, v); + await prefs.setInt(StorageKeys.theme, v); Fimber.d('write theme: $v'); notifyListeners(); } diff --git a/lib/scaffolds/common.dart b/lib/scaffolds/common.dart index 82f9f0b..82d1878 100644 --- a/lib/scaffolds/common.dart +++ b/lib/scaffolds/common.dart @@ -19,6 +19,8 @@ class CommonScaffold extends StatelessWidget { @override Widget build(BuildContext context) { final theme = Provider.of(context); + // FIXME: A hack to get brightness before MaterialApp been built + theme.setSystemBrightness(MediaQuery.of(context).platformBrightness); switch (theme.theme) { case AppThemeType.cupertino: @@ -32,7 +34,6 @@ class CommonScaffold extends StatelessWidget { default: return Scaffold( appBar: AppBar( - brightness: theme.brightnessOf(context), title: title, actions: [ if (action != null) action, diff --git a/lib/screens/object.dart b/lib/screens/object.dart index dfadd6d..736d675 100644 --- a/lib/screens/object.dart +++ b/lib/screens/object.dart @@ -170,7 +170,7 @@ class ObjectScreen extends StatelessWidget { text, language: _language, theme: themeMap[ - theme.brightnessOf(context) == Brightness.dark + theme.brightness == Brightness.dark ? codeProvider.themeDark : codeProvider.theme], padding: CommonStyle.padding, diff --git a/lib/screens/user.dart b/lib/screens/user.dart index a9f25eb..3595320 100644 --- a/lib/screens/user.dart +++ b/lib/screens/user.dart @@ -230,7 +230,7 @@ class UserScreen extends StatelessWidget { spacing: 3, children: week.contributionDays.map((day) { var color = convertColor(day.color); - if (theme.brightnessOf(context) == Brightness.dark) { + if (theme.brightness == Brightness.dark) { color = Color.fromRGBO(0xff - color.red, 0xff - color.green, 0xff - color.blue, 1); } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index e6ce56c..dd74bb5 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -15,11 +15,13 @@ export 'extensions.dart'; export 'package:flutter_vector_icons/flutter_vector_icons.dart'; class StorageKeys { - static const accounts = 'accounts'; + @deprecated static const account = 'account'; + @deprecated static const github = 'github'; + + static const accounts = 'accounts'; static const theme = 'theme'; - static const newsFilter = 'news.filter'; } class CommonStyle {