Skip to content

Commit

Permalink
add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Predidit committed Mar 25, 2024
1 parent fa3a675 commit c0469ce
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 35 deletions.
31 changes: 15 additions & 16 deletions lib/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:adaptive_theme/adaptive_theme.dart';

class AppWidget extends StatefulWidget {
const AppWidget({super.key});
Expand All @@ -13,23 +14,21 @@ class AppWidget extends StatefulWidget {
class _AppWidgetState extends State<AppWidget> {
@override
Widget build(BuildContext context) {
var app = MaterialApp.router(
title: "oneAnime",
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const [Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN")],
locale: const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN"),
theme: ThemeData(
useMaterial3: true,
primarySwatch: Colors.purple,
var app = AdaptiveTheme(
light: ThemeData.light(useMaterial3: true),
dark: ThemeData.dark(useMaterial3: true),
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => MaterialApp.router(
title: "oneAnime",
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const [Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN")],
locale: const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: "CN"),
theme: theme,
darkTheme: darkTheme,
routerConfig: Modular.routerConfig,
builder: FlutterSmartDialog.init(),
// navigatorObservers: [Asuka.asukaHeroController],
),
darkTheme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
primaryColor: Colors.purple,
),
routerConfig: Modular.routerConfig,
builder: FlutterSmartDialog.init(),
// navigatorObservers: [Asuka.asukaHeroController],
);
Modular.setObservers([FlutterSmartDialog.observer]);
return app;
Expand Down
5 changes: 2 additions & 3 deletions lib/pages/init_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';



class InitPage extends StatefulWidget {
const InitPage({super.key});

@override
State<InitPage> createState() => _InitPageState();
}
}

class _InitPageState extends State<InitPage> {
// Box setting = GStorage.setting;

@override
void initState() {
Expand Down
107 changes: 91 additions & 16 deletions lib/pages/my/my_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:oneanime/utils/storage.dart';
import 'package:oneanime/pages/my/my_controller.dart';
import 'package:hive/hive.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:adaptive_theme/adaptive_theme.dart';

class MyPage extends StatefulWidget {
const MyPage({super.key});
Expand All @@ -23,13 +24,16 @@ class _MyPageState extends State<MyPage> {
dynamic navigationBarState;
Box setting = GStorage.setting;
late dynamic defaultDanmakuArea;
late dynamic defaultThemeMode;
final _mineController = Modular.get<MyController>();

@override
void initState() {
super.initState();
defaultDanmakuArea =
setting.get(SettingBoxKey.danmakuArea, defaultValue: 1.0);
defaultThemeMode =
setting.get(SettingBoxKey.themeMode, defaultValue: 'system');
WidgetsBinding.instance.addPostFrameCallback((_) {
// 在widget构建完成后调用的函数
navigationBarState = Platform.isWindows
Expand All @@ -52,6 +56,22 @@ class _MyPageState extends State<MyPage> {
});
}

void updateTheme(String theme) async {
if (theme == 'dark') {
AdaptiveTheme.of(context).setDark();
}
if (theme == 'light') {
AdaptiveTheme.of(context).setLight();
}
if (theme == 'system') {
AdaptiveTheme.of(context).setSystem();
}
await setting.put(SettingBoxKey.themeMode, theme);
setState(() {
defaultThemeMode = theme;
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
Expand Down Expand Up @@ -101,22 +121,6 @@ class _MyPageState extends State<MyPage> {
),
ListTile(
onTap: () async {
// double? result = await showDialog(
// context: context,
// builder: (context) {
// return SelectDialog<double>(
// title: '弹幕区域',
// value: defaultDanmakuArea,
// values: [0.25, 0.5, 1.0].map((e) {
// return {'title': '$e 屏幕', 'value': e};
// }).toList());
// },
// );
// if (result != null) {
// defaultDanmakuArea = result;
// setting.put(SettingBoxKey.danmakuArea, result);
// setState(() {});
// }
final List<double> danAreaList = [
0.25,
0.5,
Expand Down Expand Up @@ -191,6 +195,77 @@ class _MyPageState extends State<MyPage> {
color: Theme.of(context).colorScheme.primary,
)),
),
ListTile(
onTap: () {
SmartDialog.show(
useAnimation: false,
builder: (context) {
return AlertDialog(
title: const Text('主题模式'),
content: StatefulBuilder(
builder:
(BuildContext context, StateSetter setState) {
return Wrap(
spacing: 8,
runSpacing: 2,
children: [
defaultThemeMode == 'system'
? FilledButton(
onPressed: () {
updateTheme('system');
SmartDialog.dismiss();
},
child: const Text("跟随系统"))
: FilledButton.tonal(
onPressed: () {
updateTheme('system');
SmartDialog.dismiss();
},
child: const Text("跟随系统")),
defaultThemeMode == 'light'
? FilledButton(
onPressed: () {
updateTheme('light');
SmartDialog.dismiss();
},
child: const Text("浅色"))
: FilledButton.tonal(
onPressed: () {
updateTheme('light');
SmartDialog.dismiss();
},
child: const Text("浅色")),
defaultThemeMode == 'dark'
? FilledButton(
onPressed: () {
updateTheme('dark');
SmartDialog.dismiss();
},
child: const Text("深色"))
: FilledButton.tonal(
onPressed: () {
updateTheme('dark');
SmartDialog.dismiss();
},
child: const Text("深色")),
],
);
},
),
);
});
},
dense: false,
title: const Text('主题模式'),
subtitle: Text(
defaultThemeMode == 'light'
? '浅色'
: (defaultThemeMode == 'dark' ? '深色' : '跟随系统'),
style: Theme.of(context)
.textTheme
.labelMedium!
.copyWith(color: Theme.of(context).colorScheme.outline)),
),
(Platform.isAndroid || Platform.isAndroid)
? const InkWell(
child: SetSwitchItem(
Expand Down
1 change: 1 addition & 0 deletions lib/utils/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ class SettingBoxKey {
danmakuScroll = 'danmakuScroll',
danmakuBottom = 'danmakuBottom',
danmakuArea = 'danmakuArea',
themeMode = 'themeMode',
autoPlay = 'autoPlay';
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import package_info_plus
import path_provider_foundation
import screen_brightness_macos
import screen_retriever
import shared_preferences_foundation
import sqflite
import url_launcher_macos
import wakelock_plus
Expand All @@ -29,6 +30,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
ScreenBrightnessMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenBrightnessMacosPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin"))
Expand Down
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "67.0.0"
adaptive_theme:
dependency: "direct main"
description:
name: adaptive_theme
sha256: f4ee609b464e5efc68131d9d15ba9aa1de4e3b5ede64be17781c6e19a52d637d
url: "https://pub.dev"
source: hosted
version: "3.6.0"
analyzer:
dependency: transitive
description:
Expand Down Expand Up @@ -982,6 +990,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.1.9"
shared_preferences:
dependency: transitive
description:
name: shared_preferences
sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "7708d83064f38060c7b39db12aefe449cb8cdc031d6062280087bc4cdb988f5c"
url: "https://pub.dev"
source: hosted
version: "2.3.5"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "7b15ffb9387ea3e237bb7a66b8a23d2147663d391cafc5c8f37b2e7b4bde5d21"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shelf:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies:
flutter_volume_controller: ^1.3.1
screen_brightness: ^0.2.2+1
wakelock_plus: ^1.1.1
adaptive_theme: ^3.6.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit c0469ce

Please sign in to comment.