From 5cf2f9f33a3d2bc7cff918c510f13f00b261f2c1 Mon Sep 17 00:00:00 2001 From: nyne Date: Sat, 25 Jan 2025 11:10:00 +0800 Subject: [PATCH] Update theme --- lib/init.dart | 14 +++++++-- lib/main.dart | 80 +++++++++++++++++++++++++++------------------------ pubspec.lock | 13 +++++++++ pubspec.yaml | 3 ++ 4 files changed, 71 insertions(+), 39 deletions(-) diff --git a/lib/init.dart b/lib/init.dart index ec4ea7d..f3f4c0d 100644 --- a/lib/init.dart +++ b/lib/init.dart @@ -1,4 +1,6 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter_saf/flutter_saf.dart'; +import 'package:rhttp/rhttp.dart'; import 'package:venera/foundation/app.dart'; import 'package:venera/foundation/cache_manager.dart'; import 'package:venera/foundation/comic_source/comic_source.dart'; @@ -8,12 +10,12 @@ import 'package:venera/foundation/js_engine.dart'; import 'package:venera/foundation/local.dart'; import 'package:venera/foundation/log.dart'; import 'package:venera/network/cookie_jar.dart'; +import 'package:venera/utils/app_links.dart'; import 'package:venera/utils/tags_translation.dart'; import 'package:venera/utils/translations.dart'; - import 'foundation/appdata.dart'; -extension FutureInit on Future { +extension _FutureInit on Future { /// Prevent unhandled exception /// /// A unhandled exception occurred in init() will cause the app to crash. @@ -27,6 +29,7 @@ extension FutureInit on Future { } Future init() async { + await Rhttp.init(); await SAFTaskWorker().init().wait(); await AppTranslation.init().wait(); await appdata.init().wait(); @@ -39,4 +42,11 @@ Future init() async { await ComicSource.init().wait(); await LocalManager().init().wait(); CacheManager().setLimitSize(appdata.settings['cacheSize']); + if (App.isAndroid) { + handleLinks(); + } + FlutterError.onError = (details) { + Log.error( + "Unhandled Exception", "${details.exception}\n${details.stack}"); + }; } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 47901dc..42c9c82 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,13 +1,13 @@ import 'dart:async'; import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:dynamic_color/dynamic_color.dart'; +import 'package:flex_seed_scheme/flex_seed_scheme.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:rhttp/rhttp.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:venera/foundation/log.dart'; import 'package:venera/pages/auth_page.dart'; import 'package:venera/pages/main_page.dart'; -import 'package:venera/utils/app_links.dart'; import 'package:venera/utils/io.dart'; import 'package:window_manager/window_manager.dart'; import 'components/components.dart'; @@ -17,21 +17,11 @@ import 'foundation/appdata.dart'; import 'init.dart'; void main(List args) { - if (runWebViewTitleBarWidget(args)) { - return; - } + if (runWebViewTitleBarWidget(args)) return; overrideIO(() { runZonedGuarded(() async { - await Rhttp.init(); WidgetsFlutterBinding.ensureInitialized(); await init(); - if (App.isAndroid) { - handleLinks(); - } - FlutterError.onError = (details) { - Log.error( - "Unhandled Exception", "${details.exception}\n${details.stack}"); - }; runApp(const MyApp()); if (App.isDesktop) { await windowManager.ensureInitialized(); @@ -54,7 +44,7 @@ void main(List args) { }); } }, (error, stack) { - Log.error("Unhandled Exception", "$error\n$stack"); + Log.error("Unhandled Exception", error, stack); }); }); } @@ -155,47 +145,63 @@ class _MyAppState extends State with WidgetsBindingObserver { home = const MainPage(); } return DynamicColorBuilder(builder: (light, dark) { + Color? primary, secondary, tertiary; if (appdata.settings['color'] != 'system' || light == null || dark == null) { - var color = translateColorSetting(); - light = ColorScheme.fromSeed( - seedColor: color, - surface: Colors.white, - ); - dark = ColorScheme.fromSeed( - seedColor: color, - brightness: Brightness.dark, - surface: Colors.black, - ); + primary = translateColorSetting(); } else { - light = ColorScheme.fromSeed( - seedColor: light.primary, - surface: Colors.white, - ); - dark = ColorScheme.fromSeed( - seedColor: dark.primary, - brightness: Brightness.dark, - surface: Colors.black, - ); + primary = light.primary; + secondary = light.secondary; + tertiary = light.tertiary; } return MaterialApp( home: home, debugShowCheckedModeBanner: false, theme: ThemeData( - colorScheme: light, - fontFamily: App.isWindows ? "Microsoft YaHei" : null, + colorScheme: SeedColorScheme.fromSeeds( + primaryKey: primary, + secondaryKey: secondary, + tertiaryKey: tertiary, + tones: FlexTones.vividBackground(Brightness.light), + ), ), navigatorKey: App.rootNavigatorKey, darkTheme: ThemeData( - colorScheme: dark, - fontFamily: App.isWindows ? "Microsoft YaHei" : null, + colorScheme: SeedColorScheme.fromSeeds( + primaryKey: primary, + secondaryKey: secondary, + tertiaryKey: tertiary, + brightness: Brightness.dark, + tones: FlexTones.vividBackground(Brightness.dark), + ), ), themeMode: switch (appdata.settings['theme_mode']) { 'light' => ThemeMode.light, 'dark' => ThemeMode.dark, _ => ThemeMode.system }, + localizationsDelegates: [ + GlobalMaterialLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + locale: () { + var lang = appdata.settings['language']; + if (lang == 'system') { + return null; + } + return switch (lang) { + 'zh-CN' => const Locale('zh', 'CN'), + 'zh-TW' => const Locale('zh', 'TW'), + 'en-US' => const Locale('en'), + _ => null + }; + }(), + supportedLocales: const [ + Locale('zh', 'CN'), + Locale('zh', 'TW'), + Locale('en'), + ], builder: (context, widget) { ErrorWidget.builder = (details) { Log.error("Unhandled Exception", diff --git a/pubspec.lock b/pubspec.lock index 749ee36..32f5111 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -274,6 +274,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" + flex_seed_scheme: + dependency: "direct main" + description: + name: flex_seed_scheme + sha256: d3ba3c5c92d2d79d45e94b4c6c71d01fac3c15017da1545880c53864da5dfeb0 + url: "https://pub.dev" + source: hosted + version: "3.5.0" flutter: dependency: "direct main" description: flutter @@ -368,6 +376,11 @@ packages: url: "https://pub.dev" source: hosted version: "5.0.0" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_memory_info: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index b93e12e..feb7eab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -72,6 +72,9 @@ dependencies: git: url: https://github.com/wgh136/flutter_7zip ref: b33344797f1d2469339e0e1b75f5f954f1da224c + flex_seed_scheme: ^3.5.0 + flutter_localizations: + sdk: flutter dev_dependencies: flutter_test: