Add dark mode support

This commit is contained in:
2023-09-01 13:22:05 +08:00
parent 45fb64f4af
commit 75859e7055
4 changed files with 72 additions and 7 deletions

View File

@@ -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!'),
),
);

View File

@@ -5,5 +5,6 @@
"username": "Username",
"password": "Password",
"save": "Save",
"login": "Login"
"login": "Login",
"titleBar": "EH Downloader"
}

View File

@@ -5,5 +5,6 @@
"username": "用户名",
"password": "密码",
"save": "保存",
"login": "登录"
"login": "登录",
"titleBar": "EH 下载器"
}

View File

@@ -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<MainApp> createState() => _MainApp();
// ignore: library_private_types_in_public_api
static _MainApp of(BuildContext context) =>
context.findAncestorStateOfType<_MainApp>()!;
}
class _MainApp extends State<MainApp> {
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;
});
}
}