From 75859e705532dd922a4f05d71fcdb6c86424b392 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 1 Sep 2023 13:22:05 +0800 Subject: [PATCH] Add dark mode support --- lib/home.dart | 30 ++++++++++++++++++++++++++++-- lib/l10n/app_en.arb | 3 ++- lib/l10n/app_zh.arb | 3 ++- lib/main.dart | 43 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/lib/home.dart b/lib/home.dart index c45655d..751d984 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -1,11 +1,19 @@ import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:logging/logging.dart'; import 'globals.dart'; +import 'main.dart'; final _log = Logger("HomePage"); +ThemeMode _themeModeNext(ThemeMode mode) { + if (mode == ThemeMode.system) return ThemeMode.light; + if (mode == ThemeMode.dark) return ThemeMode.system; + return ThemeMode.dark; +} + class HomePage extends HookWidget { const HomePage({Key? key}) : super(key: key); @@ -26,8 +34,26 @@ class HomePage extends HookWidget { } return; }, []); - return const Scaffold( - body: Center( + var mode = useState(MainApp.of(context).themeMode); + return Scaffold( + appBar: AppBar( + title: Text(AppLocalizations.of(context)!.titleBar), + actions: [ + IconButton( + onPressed: () { + final n = _themeModeNext(mode.value); + MainApp.of(context).changeThemeMode(n); + mode.value = n; + }, + icon: Icon(mode.value == ThemeMode.system + ? Icons.brightness_auto + : mode.value == ThemeMode.dark + ? Icons.dark_mode + : Icons.light_mode)), + const IconButton(onPressed: null, icon: Icon(Icons.more_vert)), + ], + ), + body: const Center( child: Text('Hello World!'), ), ); diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index a8f2381..f0ff564 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -5,5 +5,6 @@ "username": "Username", "password": "Password", "save": "Save", - "login": "Login" + "login": "Login", + "titleBar": "EH Downloader" } diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb index 438c0fa..4f75f38 100644 --- a/lib/l10n/app_zh.arb +++ b/lib/l10n/app_zh.arb @@ -5,5 +5,6 @@ "username": "用户名", "password": "密码", "save": "保存", - "login": "登录" + "login": "登录", + "titleBar": "EH 下载器" } diff --git a/lib/main.dart b/lib/main.dart index 79f0c85..01c4fa0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -75,12 +75,40 @@ void main() async { runApp(const MainApp()); } -class MainApp extends StatelessWidget { +final _log = Logger("MainApp"); + +class MainApp extends StatefulWidget { const MainApp({super.key}); + @override + State createState() => _MainApp(); + // ignore: library_private_types_in_public_api + static _MainApp of(BuildContext context) => + context.findAncestorStateOfType<_MainApp>()!; +} + +class _MainApp extends State { + ThemeMode _themeMode = ThemeMode.system; + ThemeData _themeData = ThemeData(); + ThemeData _darkThemeData = ThemeData.dark(); + ThemeMode get themeMode => _themeMode; + + @override + void initState() { + super.initState(); + try { + _themeMode = ThemeMode.values[prefs.getInt("themeMode") ?? 0]; + } catch (e) { + _log.warning("Failed to read themeMode from prefs:", e); + } + if (kIsWeb || isWindows) { + _themeData = _themeData.useSystemChineseFont(); + _darkThemeData = _darkThemeData.useSystemChineseFont(); + } + } + @override Widget build(BuildContext context) { - var theme = ThemeData(); return MaterialApp.router( routerConfig: _router, onGenerateTitle: (context) { @@ -92,7 +120,16 @@ class MainApp extends StatelessWidget { }, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, - theme: (kIsWeb || isWindows) ? theme.useSystemChineseFont() : theme, + theme: _themeData, + darkTheme: _darkThemeData, + themeMode: _themeMode, ); } + + void changeThemeMode(ThemeMode mode) { + prefs.setInt("themeMode", mode.index); + setState(() { + _themeMode = mode; + }); + } }