120 lines
3.5 KiB
Dart
120 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'pages/battery_health_page.dart';
|
|
import 'services/saf_storage_service.dart';
|
|
|
|
// 🆕 1.7.0 全局 Key,用于从子页面切换主题
|
|
final GlobalKey<_MyAppState> appKey = GlobalKey<_MyAppState>();
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await SafStorageService().init();
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final themeModeIndex = prefs.getInt('themeMode') ?? 0;
|
|
final themeMode =
|
|
ThemeMode.values[themeModeIndex.clamp(0, ThemeMode.values.length - 1)];
|
|
|
|
runApp(MyApp(key: appKey, initialThemeMode: themeMode));
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
final ThemeMode initialThemeMode;
|
|
const MyApp({super.key, required this.initialThemeMode});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
late ThemeMode _themeMode;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_themeMode = widget.initialThemeMode;
|
|
}
|
|
|
|
void switchTheme(ThemeMode mode) {
|
|
setState(() {
|
|
_themeMode = mode;
|
|
});
|
|
SharedPreferences.getInstance().then((prefs) {
|
|
prefs.setInt('themeMode', mode.index);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: '电池健康度',
|
|
theme: _lightTheme(),
|
|
darkTheme: _darkTheme(),
|
|
themeMode: _themeMode,
|
|
home: const BatteryHealthPage(),
|
|
debugShowCheckedModeBanner: false,
|
|
);
|
|
}
|
|
|
|
ThemeData _lightTheme() {
|
|
return ThemeData(
|
|
fontFamily: 'HarmonyOS_Sans_SC',
|
|
fontFamilyFallback: const ['Flyme'],
|
|
useMaterial3: true,
|
|
brightness: Brightness.light,
|
|
scaffoldBackgroundColor: Colors.white,
|
|
appBarTheme: const AppBarTheme(
|
|
backgroundColor: Colors.white,
|
|
foregroundColor: Colors.black,
|
|
elevation: 0,
|
|
centerTitle: false,
|
|
),
|
|
iconTheme: const IconThemeData(color: Colors.black),
|
|
colorScheme: const ColorScheme.light(
|
|
primary: Color(0xFF1A73E8),
|
|
secondary: Color(0xFF1A73E8),
|
|
surface: Colors.white,
|
|
onSurface: Colors.black, // 🟢 主要文字色
|
|
onSurfaceVariant: Colors.black87,
|
|
),
|
|
textTheme: const TextTheme(
|
|
bodyLarge: TextStyle(color: Colors.black),
|
|
bodyMedium: TextStyle(color: Colors.black87),
|
|
labelLarge: TextStyle(color: Colors.black),
|
|
),
|
|
dividerColor: Colors.grey.shade200,
|
|
);
|
|
}
|
|
|
|
ThemeData _darkTheme() {
|
|
return ThemeData(
|
|
fontFamily: 'HarmonyOS_Sans_SC',
|
|
fontFamilyFallback: const ['Flyme'],
|
|
useMaterial3: true,
|
|
brightness: Brightness.dark,
|
|
scaffoldBackgroundColor: const Color(0xFF121212),
|
|
appBarTheme: const AppBarTheme(
|
|
backgroundColor: Color(0xFF1E1E1E),
|
|
foregroundColor: Colors.white,
|
|
elevation: 0,
|
|
centerTitle: false,
|
|
),
|
|
iconTheme: const IconThemeData(color: Colors.white),
|
|
colorScheme: const ColorScheme.dark(
|
|
primary: Color(0xFF4FC3F7),
|
|
secondary: Color(0xFF4FC3F7),
|
|
surface: Color(0xFF121212),
|
|
onSurface: Colors.white, // 🟢 主要文字色(夜间模式为白色)
|
|
onSurfaceVariant: Colors.white70,
|
|
),
|
|
textTheme: const TextTheme(
|
|
bodyLarge: TextStyle(color: Colors.white),
|
|
bodyMedium: TextStyle(color: Colors.white70),
|
|
labelLarge: TextStyle(color: Colors.white),
|
|
),
|
|
dividerColor: Colors.grey.shade800,
|
|
);
|
|
}
|
|
}
|