theme_provider.dart
3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import 'dart:ui';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:one_poem/res/constant.dart';
import 'package:one_poem/res/resources.dart';
import 'package:one_poem/routers/web_page_transitions.dart';
extension ThemeModeExtension on ThemeMode {
String get value => <String>['System', 'Light', 'Dark'][index];
}
class ThemeProvider extends ChangeNotifier {
void syncTheme() {
final String theme = SpUtil.getString(Constant.theme) ?? '';
if (theme.isNotEmpty && theme != ThemeMode.system.value) {
notifyListeners();
}
}
void setTheme(ThemeMode themeMode) {
SpUtil.putString(Constant.theme, themeMode.value);
notifyListeners();
}
ThemeMode getThemeMode(){
final String theme = SpUtil.getString(Constant.theme) ?? '';
switch(theme) {
case 'Dark':
return ThemeMode.dark;
case 'Light':
return ThemeMode.light;
default:
return ThemeMode.system;
}
}
ThemeData getTheme({bool isDarkMode = false}) {
return ThemeData(
errorColor: isDarkMode ? Colours.dark_red : Colours.red,
primaryColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
colorScheme: ColorScheme.fromSwatch().copyWith(
brightness: isDarkMode ? Brightness.dark : Brightness.light,
secondary: isDarkMode ? Colours.dark_app_main : Colours.app_main,
),
// Tab指示器颜色
indicatorColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
// 页面背景色
scaffoldBackgroundColor: isDarkMode ? Colours.dark_bg_color : Colors.white,
// 主要用于Material背景色
canvasColor: isDarkMode ? Colours.dark_material_bg : Colors.white,
// 文字选择色(输入框选择文字等)
// textSelectionColor: Colours.app_main.withAlpha(70),
// textSelectionHandleColor: Colours.app_main,
// 稳定版:1.23 变更(https://flutter.dev/docs/release/breaking-changes/text-selection-theme)
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colours.app_main.withAlpha(70),
selectionHandleColor: Colours.app_main,
cursorColor: Colours.app_main,
),
textTheme: TextTheme(
// TextField输入文字颜色
subtitle1: isDarkMode ? TextStyles.textDark : TextStyles.text,
// Text文字样式
bodyText2: isDarkMode ? TextStyles.textDark : TextStyles.text,
subtitle2: isDarkMode ? TextStyles.textDarkGray12 : TextStyles.textGray12,
),
inputDecorationTheme: InputDecorationTheme(
hintStyle: isDarkMode ? TextStyles.textHint14 : TextStyles.textDarkGray14,
),
appBarTheme: AppBarTheme(
elevation: 0.0,
color: isDarkMode ? Colours.dark_bg_color : Colors.white,
systemOverlayStyle: isDarkMode ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
),
dividerTheme: DividerThemeData(
color: isDarkMode ? Colours.dark_line : Colours.line,
space: 0.6,
thickness: 0.6
),
cupertinoOverrideTheme: CupertinoThemeData(
brightness: isDarkMode ? Brightness.dark : Brightness.light,
),
pageTransitionsTheme: NoTransitionsOnWeb(),
visualDensity: VisualDensity.standard, // https://github.com/flutter/flutter/issues/77142
);
}
}