fix: follow system brightness

This commit is contained in:
Rongjian Zhang 2020-01-16 12:45:04 +08:00
parent 5c6428a36d
commit fccb79b2cc
7 changed files with 24 additions and 29 deletions

View File

@ -16,12 +16,7 @@ class MyApp extends StatelessWidget {
); );
default: default:
return MaterialApp( return MaterialApp(
theme: ThemeData( theme: ThemeData(brightness: theme.brightness),
brightness: theme.brightness,
// primaryColorBrightness: theme.brightness,
primaryColorLight: theme.paletteLight.background,
primaryColorDark: theme.paletteDark.background,
),
home: Home(), home: Home(),
); );
} }

View File

@ -137,7 +137,6 @@ class _HomeState extends State<Home> {
}); });
}, },
); );
default: default:
return Scaffold( return Scaffold(
body: _buildScreen(active), body: _buildScreen(active),

View File

@ -97,28 +97,26 @@ class Palette {
} }
class ThemeModel with ChangeNotifier { class ThemeModel with ChangeNotifier {
static const kTheme = 'theme';
static const kBrightness = 'brightness'; static const kBrightness = 'brightness';
int _theme; int _theme;
int get theme => _theme; int get theme => _theme;
bool get ready => _theme != null; bool get ready => _theme != null;
int _brightnessValue = AppBrightnessType.followSystem; Brightness systemBrightness = Brightness.light;
int get brighnessValue => _brightnessValue; void setSystemBrightness(Brightness v) {
// print('systemBrightness: $v');
/// not null if (v != systemBrightness) {
Brightness brightnessOf(BuildContext context) { Future.microtask(() {
switch (_brightnessValue) { systemBrightness = v;
case AppBrightnessType.light: notifyListeners();
return Brightness.light; });
case AppBrightnessType.dark:
return Brightness.dark;
default:
return MediaQuery.of(context).platformBrightness;
} }
} }
int _brightnessValue = AppBrightnessType.followSystem;
int get brighnessValue => _brightnessValue;
// could be null // could be null
Brightness get brightness { Brightness get brightness {
switch (_brightnessValue) { switch (_brightnessValue) {
@ -127,7 +125,7 @@ class ThemeModel with ChangeNotifier {
case AppBrightnessType.dark: case AppBrightnessType.dark:
return Brightness.dark; return Brightness.dark;
default: default:
return null; return systemBrightness;
} }
} }
@ -161,7 +159,7 @@ class ThemeModel with ChangeNotifier {
); );
Palette paletteOf(BuildContext context) { Palette paletteOf(BuildContext context) {
switch (brightnessOf(context)) { switch (brightness) {
case Brightness.light: case Brightness.light:
return paletteLight; return paletteLight;
case Brightness.dark: case Brightness.dark:
@ -173,7 +171,7 @@ class ThemeModel with ChangeNotifier {
Future<void> init() async { Future<void> init() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final v = prefs.getInt(kTheme); final v = prefs.getInt(StorageKeys.theme);
Fimber.d('read theme: $v'); Fimber.d('read theme: $v');
if (AppThemeType.values.contains(v)) { if (AppThemeType.values.contains(v)) {
_theme = v; _theme = v;
@ -194,7 +192,7 @@ class ThemeModel with ChangeNotifier {
Future<void> setTheme(int v) async { Future<void> setTheme(int v) async {
_theme = v; _theme = v;
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
await prefs.setInt(kTheme, v); await prefs.setInt(StorageKeys.theme, v);
Fimber.d('write theme: $v'); Fimber.d('write theme: $v');
notifyListeners(); notifyListeners();
} }

View File

@ -19,6 +19,8 @@ class CommonScaffold extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context); final theme = Provider.of<ThemeModel>(context);
// FIXME: A hack to get brightness before MaterialApp been built
theme.setSystemBrightness(MediaQuery.of(context).platformBrightness);
switch (theme.theme) { switch (theme.theme) {
case AppThemeType.cupertino: case AppThemeType.cupertino:
@ -32,7 +34,6 @@ class CommonScaffold extends StatelessWidget {
default: default:
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
brightness: theme.brightnessOf(context),
title: title, title: title,
actions: [ actions: [
if (action != null) action, if (action != null) action,

View File

@ -170,7 +170,7 @@ class ObjectScreen extends StatelessWidget {
text, text,
language: _language, language: _language,
theme: themeMap[ theme: themeMap[
theme.brightnessOf(context) == Brightness.dark theme.brightness == Brightness.dark
? codeProvider.themeDark ? codeProvider.themeDark
: codeProvider.theme], : codeProvider.theme],
padding: CommonStyle.padding, padding: CommonStyle.padding,

View File

@ -230,7 +230,7 @@ class UserScreen extends StatelessWidget {
spacing: 3, spacing: 3,
children: week.contributionDays.map((day) { children: week.contributionDays.map((day) {
var color = convertColor(day.color); var color = convertColor(day.color);
if (theme.brightnessOf(context) == Brightness.dark) { if (theme.brightness == Brightness.dark) {
color = Color.fromRGBO(0xff - color.red, color = Color.fromRGBO(0xff - color.red,
0xff - color.green, 0xff - color.blue, 1); 0xff - color.green, 0xff - color.blue, 1);
} }

View File

@ -15,11 +15,13 @@ export 'extensions.dart';
export 'package:flutter_vector_icons/flutter_vector_icons.dart'; export 'package:flutter_vector_icons/flutter_vector_icons.dart';
class StorageKeys { class StorageKeys {
static const accounts = 'accounts'; @deprecated
static const account = 'account'; static const account = 'account';
@deprecated
static const github = 'github'; static const github = 'github';
static const accounts = 'accounts';
static const theme = 'theme'; static const theme = 'theme';
static const newsFilter = 'news.filter';
} }
class CommonStyle { class CommonStyle {