Skip to content

Commit 7718b1c

Browse files
committed
动态取色的判断
1 parent 66efda3 commit 7718b1c

File tree

8 files changed

+124
-64
lines changed

8 files changed

+124
-64
lines changed

lib/common/provider/settings_provider.dart

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class SettingsNotifier extends StateNotifier<Settings> {
7979
state = state.copyWith(themeColorLabel: value);
8080
hiveHelper.setSettings(state);
8181
}
82+
83+
void setSupportDynamicColors(bool value) {
84+
state = state.copyWith(supportDynamicColors: value);
85+
hiveHelper.setSettings(state);
86+
}
8287
}
8388

8489
final settingsProvider =

lib/component/models/settings.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:eros_n/common/enum.dart';
2+
import 'package:eros_n/component/theme/theme.dart';
23
import 'package:eros_n/network/enum.dart';
34
import 'package:flutter/material.dart';
45
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -20,7 +21,8 @@ class Settings with _$Settings {
2021
@Default(ReadModel.leftToRight) ReadModel readModel,
2122
@Default(ListModel.waterfall) ListModel listModel,
2223
@Default('') String localeCode,
23-
@Default('dynamic') String themeColorLabel,
24+
@Default(ThemeConfig.dynamicThemeColorLabel) String themeColorLabel,
25+
@Default(false) bool supportDynamicColors,
2426
}) = _Settings;
2527

2628
const Settings._();

lib/component/models/settings.freezed.dart

+30-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mixin _$Settings {
3232
ListModel get listModel => throw _privateConstructorUsedError;
3333
String get localeCode => throw _privateConstructorUsedError;
3434
String get themeColorLabel => throw _privateConstructorUsedError;
35+
bool get supportDynamicColors => throw _privateConstructorUsedError;
3536

3637
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
3738
@JsonKey(ignore: true)
@@ -55,7 +56,8 @@ abstract class $SettingsCopyWith<$Res> {
5556
ReadModel readModel,
5657
ListModel listModel,
5758
String localeCode,
58-
String themeColorLabel});
59+
String themeColorLabel,
60+
bool supportDynamicColors});
5961
}
6062

6163
/// @nodoc
@@ -80,6 +82,7 @@ class _$SettingsCopyWithImpl<$Res> implements $SettingsCopyWith<$Res> {
8082
Object? listModel = freezed,
8183
Object? localeCode = freezed,
8284
Object? themeColorLabel = freezed,
85+
Object? supportDynamicColors = freezed,
8386
}) {
8487
return _then(_value.copyWith(
8588
isCoverBlur: isCoverBlur == freezed
@@ -130,6 +133,10 @@ class _$SettingsCopyWithImpl<$Res> implements $SettingsCopyWith<$Res> {
130133
? _value.themeColorLabel
131134
: themeColorLabel // ignore: cast_nullable_to_non_nullable
132135
as String,
136+
supportDynamicColors: supportDynamicColors == freezed
137+
? _value.supportDynamicColors
138+
: supportDynamicColors // ignore: cast_nullable_to_non_nullable
139+
as bool,
133140
));
134141
}
135142
}
@@ -152,7 +159,8 @@ abstract class _$$_SettingsCopyWith<$Res> implements $SettingsCopyWith<$Res> {
152159
ReadModel readModel,
153160
ListModel listModel,
154161
String localeCode,
155-
String themeColorLabel});
162+
String themeColorLabel,
163+
bool supportDynamicColors});
156164
}
157165

158166
/// @nodoc
@@ -179,6 +187,7 @@ class __$$_SettingsCopyWithImpl<$Res> extends _$SettingsCopyWithImpl<$Res>
179187
Object? listModel = freezed,
180188
Object? localeCode = freezed,
181189
Object? themeColorLabel = freezed,
190+
Object? supportDynamicColors = freezed,
182191
}) {
183192
return _then(_$_Settings(
184193
isCoverBlur: isCoverBlur == freezed
@@ -229,6 +238,10 @@ class __$$_SettingsCopyWithImpl<$Res> extends _$SettingsCopyWithImpl<$Res>
229238
? _value.themeColorLabel
230239
: themeColorLabel // ignore: cast_nullable_to_non_nullable
231240
as String,
241+
supportDynamicColors: supportDynamicColors == freezed
242+
? _value.supportDynamicColors
243+
: supportDynamicColors // ignore: cast_nullable_to_non_nullable
244+
as bool,
232245
));
233246
}
234247
}
@@ -248,7 +261,8 @@ class _$_Settings extends _Settings {
248261
this.readModel = ReadModel.leftToRight,
249262
this.listModel = ListModel.waterfall,
250263
this.localeCode = '',
251-
this.themeColorLabel = 'dynamic'})
264+
this.themeColorLabel = ThemeConfig.dynamicThemeColorLabel,
265+
this.supportDynamicColors = false})
252266
: super._();
253267

254268
factory _$_Settings.fromJson(Map<String, dynamic> json) =>
@@ -290,10 +304,13 @@ class _$_Settings extends _Settings {
290304
@override
291305
@JsonKey()
292306
final String themeColorLabel;
307+
@override
308+
@JsonKey()
309+
final bool supportDynamicColors;
293310

294311
@override
295312
String toString() {
296-
return 'Settings(isCoverBlur: $isCoverBlur, isTagTranslate: $isTagTranslate, dynamicColor: $dynamicColor, searchSort: $searchSort, showTags: $showTags, tagLayoutOnItem: $tagLayoutOnItem, themeMode: $themeMode, fullScreenReader: $fullScreenReader, readModel: $readModel, listModel: $listModel, localeCode: $localeCode, themeColorLabel: $themeColorLabel)';
313+
return 'Settings(isCoverBlur: $isCoverBlur, isTagTranslate: $isTagTranslate, dynamicColor: $dynamicColor, searchSort: $searchSort, showTags: $showTags, tagLayoutOnItem: $tagLayoutOnItem, themeMode: $themeMode, fullScreenReader: $fullScreenReader, readModel: $readModel, listModel: $listModel, localeCode: $localeCode, themeColorLabel: $themeColorLabel, supportDynamicColors: $supportDynamicColors)';
297314
}
298315

299316
@override
@@ -320,7 +337,9 @@ class _$_Settings extends _Settings {
320337
const DeepCollectionEquality()
321338
.equals(other.localeCode, localeCode) &&
322339
const DeepCollectionEquality()
323-
.equals(other.themeColorLabel, themeColorLabel));
340+
.equals(other.themeColorLabel, themeColorLabel) &&
341+
const DeepCollectionEquality()
342+
.equals(other.supportDynamicColors, supportDynamicColors));
324343
}
325344

326345
@JsonKey(ignore: true)
@@ -338,7 +357,8 @@ class _$_Settings extends _Settings {
338357
const DeepCollectionEquality().hash(readModel),
339358
const DeepCollectionEquality().hash(listModel),
340359
const DeepCollectionEquality().hash(localeCode),
341-
const DeepCollectionEquality().hash(themeColorLabel));
360+
const DeepCollectionEquality().hash(themeColorLabel),
361+
const DeepCollectionEquality().hash(supportDynamicColors));
342362

343363
@JsonKey(ignore: true)
344364
@override
@@ -366,7 +386,8 @@ abstract class _Settings extends Settings {
366386
final ReadModel readModel,
367387
final ListModel listModel,
368388
final String localeCode,
369-
final String themeColorLabel}) = _$_Settings;
389+
final String themeColorLabel,
390+
final bool supportDynamicColors}) = _$_Settings;
370391
const _Settings._() : super._();
371392

372393
factory _Settings.fromJson(Map<String, dynamic> json) = _$_Settings.fromJson;
@@ -396,6 +417,8 @@ abstract class _Settings extends Settings {
396417
@override
397418
String get themeColorLabel;
398419
@override
420+
bool get supportDynamicColors;
421+
@override
399422
@JsonKey(ignore: true)
400423
_$$_SettingsCopyWith<_$_Settings> get copyWith =>
401424
throw _privateConstructorUsedError;

lib/component/models/settings.g.dart

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/component/theme/theme.dart

+21-20
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@ class ThemeConfig {
77
static late ThemeData lightTheme;
88
static late ThemeData darkTheme;
99

10+
static const defaultThemeColorLabel = 'blue';
11+
static const dynamicThemeColorLabel = 'dynamic';
1012
static const Map<String, Color?> colorMap = {
11-
'dynamic': null,
12-
'red': Color(0xFFE53935),
13-
'pink': Color(0xFFD81B60),
14-
'purple': Color(0xFF8E24AA),
15-
'deep_purple': Color(0xFF5E35B1),
16-
'indigo': Color(0xFF3949AB),
17-
'blue': Color(0xFF1E88E5),
18-
'light_blue': Color(0xFF039BE5),
19-
'cyan': Color(0xFF00ACC1),
20-
'teal': Color(0xFF00897B),
21-
'green': Color(0xFF43A047),
22-
'light_green': Color(0xFF7CB342),
23-
'lime': Color(0xFFC0CA33),
24-
'yellow': Color(0xFFFDD835),
25-
'amber': Color(0xFFFFB300),
26-
'orange': Color(0xFFF4511E),
27-
'deep_orange': Color(0xFFE64A19),
28-
'brown': Color(0xFF6D4C41),
29-
'grey': Color(0xFF757575),
30-
'blue_grey': Color(0xFF546E7A),
13+
'red': Colors.red,
14+
'pink': Colors.pink,
15+
'purple': Colors.purple,
16+
'deep_purple': Colors.deepPurple,
17+
'indigo': Colors.indigo,
18+
'blue': Colors.blue,
19+
'light_blue': Colors.lightBlue,
20+
'cyan': Colors.cyan,
21+
'teal': Colors.teal,
22+
'green': Colors.green,
23+
'light_green': Colors.lightGreen,
24+
'lime': Colors.lime,
25+
'yellow': Colors.yellow,
26+
'amber': Colors.amber,
27+
'orange': Colors.orange,
28+
'deep_orange': Colors.deepOrange,
29+
'brown': Colors.brown,
30+
'grey': Colors.grey,
31+
'blue_grey': Colors.blueGrey,
3132
};
3233
}

lib/component/widget/broken_shield.dart

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@ import '../../utils/logger.dart';
1717
class BrokenShield extends StatefulWidget {
1818
const BrokenShield({super.key, required this.child});
1919

20-
final Widget child;
20+
final Widget? child;
2121

2222
@override
2323
State<BrokenShield> createState() => _BrokenShieldState();
24+
25+
static TransitionBuilder init({
26+
TransitionBuilder? builder,
27+
}) {
28+
return (BuildContext context, Widget? child) {
29+
if (builder == null) {
30+
return BrokenShield(child: child);
31+
} else {
32+
return builder(context, BrokenShield(child: child));
33+
}
34+
};
35+
}
2436
}
2537

2638
class _BrokenShieldState extends State<BrokenShield> {
@@ -132,7 +144,7 @@ class _BrokenShieldState extends State<BrokenShield> {
132144
key: overlay,
133145
initialEntries: [
134146
OverlayEntry(
135-
builder: (BuildContext context) => widget.child,
147+
builder: (BuildContext context) => widget.child ?? Container(),
136148
),
137149
// OverlayEntry(
138150
// opaque: true,

lib/main.dart

+20-19
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,21 @@ Future<void> main() async {
2121
await Global.init();
2222

2323
initLogger();
24-
runApp(ProviderScope(
25-
child: MyApp(),
24+
runApp(const ProviderScope(
2625
observers: [
2726
// LoggerObserver(),
2827
],
28+
child: MyApp(),
2929
));
3030
// runApp(ProviderScope(child: MyApp()));
3131
}
3232

33-
bool _isDemoUsingDynamicColors = false;
34-
35-
// const _brandBlue = Colors.blue;
36-
3733
class MyApp extends HookConsumerWidget {
3834
const MyApp({super.key});
3935

4036
@override
4137
Widget build(BuildContext context, WidgetRef ref) {
4238
logger.v('build MyApp');
43-
44-
// final dynamicColor =
45-
// ref.watch(settingsProvider.select((settings) => settings.dynamicColor));
4639
final themeMode =
4740
ref.watch(settingsProvider.select((settings) => settings.themeMode));
4841

@@ -52,20 +45,28 @@ class MyApp extends HookConsumerWidget {
5245
final themeColorLabel = ref
5346
.watch(settingsProvider.select((settings) => settings.themeColorLabel));
5447

55-
final dynamicColor = themeColorLabel == 'dynamic';
56-
final themeSeedColor =
57-
ThemeConfig.colorMap[themeColorLabel] ?? ThemeConfig.colorMap['blue']!;
48+
final dynamicColor = themeColorLabel == ThemeConfig.dynamicThemeColorLabel;
49+
final themeSeedColor = ThemeConfig.colorMap[themeColorLabel] ??
50+
ThemeConfig.colorMap[ThemeConfig.defaultThemeColorLabel]!;
5851

5952
return DynamicColorBuilder(
6053
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
6154
ColorScheme lightColorScheme;
6255
ColorScheme darkColorScheme;
6356

64-
if (lightDynamic != null && darkDynamic != null && dynamicColor) {
57+
final supportDynamicColors =
58+
lightDynamic != null && darkDynamic != null;
59+
60+
logger.d('supportDynamicColors: $supportDynamicColors');
61+
WidgetsBinding.instance.addPostFrameCallback((_) {
62+
ref
63+
.read(settingsProvider.notifier)
64+
.setSupportDynamicColors(supportDynamicColors);
65+
});
66+
67+
if (supportDynamicColors && dynamicColor) {
6568
lightColorScheme = lightDynamic.harmonized();
6669
darkColorScheme = darkDynamic.harmonized();
67-
68-
_isDemoUsingDynamicColors = true;
6970
} else {
7071
lightColorScheme = ColorScheme.fromSeed(
7172
seedColor: themeSeedColor,
@@ -115,10 +116,10 @@ class MyApp extends HookConsumerWidget {
115116
FlutterSmartDialog.observer,
116117
],
117118
),
118-
builder: (BuildContext context, Widget? child) {
119-
return BrokenShield(
120-
child: FlutterSmartDialog.init()(context, child));
121-
},
119+
builder: FlutterSmartDialog.init(
120+
styleBuilder: (child) => child,
121+
builder: BrokenShield.init(),
122+
),
122123
locale: locale(localeCode),
123124
onGenerateTitle: (BuildContext context) => L10n.of(context).app_title,
124125
// debugShowCheckedModeBanner: false,

0 commit comments

Comments
 (0)