Browse Source

v008fix2

weicu
wolfplus 2 years ago
parent
commit
133f011a34
  1. 17
      lib/app/theme/theme_service.dart
  2. 34
      lib/main.dart
  3. 20
      lib/modules/settings/controllers/settings_controller.dart
  4. 13
      lib/modules/settings/views/settings_view.dart
  5. 12
      lib/modules/splash/controllers/splash_controller.dart
  6. 5
      lib/modules/translation/controllers/translation_controller.dart

17
lib/app/theme/theme_service.dart

@ -6,24 +6,23 @@ class ThemeService {
final _box = GetStorage(); final _box = GetStorage();
final _key = 'isDarkMode'; final _key = 'isDarkMode';
/// 获取主题模式 - 如果没有保存过,则默认为系统主题 /// 获取主题模式
ThemeMode getThemeMode() { ThemeMode getThemeMode() {
return isSavedDarkMode() ? ThemeMode.dark : ThemeMode.light; final isDarkMode = isSavedDarkMode();
print('获取保存的主题: ${isDarkMode ? "暗黑" : "浅色"}');
return isDarkMode ? ThemeMode.dark : ThemeMode.light;
} }
/// 是否是暗黑模式 /// 是否是暗黑模式
bool isSavedDarkMode() { bool isSavedDarkMode() {
return _box.read(_key) ?? false; final value = _box.read(_key);
print('从存储读取主题设置: $value');
return value ?? false;
} }
/// 保存主题模式到本地存储 /// 保存主题模式到本地存储
void saveThemeMode(bool isDarkMode) { void saveThemeMode(bool isDarkMode) {
print('保存主题设置: ${isDarkMode ? "暗黑" : "浅色"}');
_box.write(_key, isDarkMode); _box.write(_key, isDarkMode);
} }
/// 切换主题
void changeThemeMode() {
Get.changeThemeMode(isSavedDarkMode() ? ThemeMode.light : ThemeMode.dark);
saveThemeMode(!isSavedDarkMode());
}
} }

34
lib/main.dart

@ -16,15 +16,12 @@ void main() async {
// 确保Flutter绑定初始化 // 确保Flutter绑定初始化
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
// 先运行应用,显示启动页面 // 初始化存储 - 在创建应用之前初始化
runApp(const MainApp()); await GetStorage.init();
// 然后异步执行其他初始化操作 // 初始化环境变量
_initializeApp(); await dotenv.load();
}
// 异步初始化应用
Future<void> _initializeApp() async {
// 设置窗口大小(仅在 macOS 下生效) // 设置窗口大小(仅在 macOS 下生效)
if (Platform.isMacOS) { if (Platform.isMacOS) {
const double width = 375.0; const double width = 375.0;
@ -35,7 +32,7 @@ Future<void> _initializeApp() async {
setWindowMaxSize(const Size(width, height)); setWindowMaxSize(const Size(width, height));
// 居中显示窗口 // 居中显示窗口
getCurrentScreen().then((screen) { final screen = await getCurrentScreen();
if (screen != null) { if (screen != null) {
final screenFrame = screen.frame; final screenFrame = screen.frame;
final windowFrame = Rect.fromCenter( final windowFrame = Rect.fromCenter(
@ -45,22 +42,10 @@ Future<void> _initializeApp() async {
); );
setWindowFrame(windowFrame); setWindowFrame(windowFrame);
} }
});
} }
// 初始化环境变量 // 运行应用
await dotenv.load(); runApp(const MainApp());
// 初始化存储
await GetStorage.init();
// 注意:应用启动时会在 SplashController 中检查上次保存的皮肤模式
// 并自动应用保存的主题(暗黑/浅色)
// AudioService 将在 AudioServiceManager 中初始化
// 不再需要在这里调用 AudioService.init
// 其他可能的初始化操作...
} }
class MainApp extends StatelessWidget { class MainApp extends StatelessWidget {
@ -68,6 +53,9 @@ class MainApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// 获取保存的主题设置
final themeMode = ThemeService().getThemeMode();
return ScreenUtilInit( return ScreenUtilInit(
designSize: const Size(375, 812), designSize: const Size(375, 812),
minTextAdapt: true, minTextAdapt: true,
@ -77,7 +65,7 @@ class MainApp extends StatelessWidget {
title: 'Deep Voice', title: 'Deep Voice',
theme: AppTheme.lightTheme, theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme, darkTheme: AppTheme.darkTheme,
themeMode: ThemeService().getThemeMode(), // 初始主题模式,将在 SplashController 中再次检查 themeMode: themeMode, // 使用提前获取的主题模式
initialBinding: InitialBinding(), initialBinding: InitialBinding(),
initialRoute: Routes.splash, initialRoute: Routes.splash,
getPages: AppPages.pages, getPages: AppPages.pages,

20
lib/modules/settings/controllers/settings_controller.dart

@ -17,13 +17,27 @@ class SettingsController extends GetxController {
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
isDarkMode.value = _themeService.isSavedDarkMode();
// 从存储中加载上次保存的主题设置
final savedDarkMode = _themeService.isSavedDarkMode();
// 设置当前主题状态
isDarkMode.value = savedDarkMode;
} }
// 切换主题 // 切换主题
void toggleTheme() { void toggleTheme() {
_themeService.changeThemeMode(); // 切换暗黑模式状态
isDarkMode.value = _themeService.isSavedDarkMode(); isDarkMode.value = !isDarkMode.value;
// 更新全局主题
Get.changeThemeMode(isDarkMode.value ? ThemeMode.dark : ThemeMode.light);
// 保存用户选择的主题设置 - 确保立即写入存储
_themeService.saveThemeMode(isDarkMode.value);
// 打印日志,确认主题已保存
print('主题已切换,当前模式: ${isDarkMode.value ? "暗黑" : "浅色"}');
} }
void setSourceLanguage(String language) { void setSourceLanguage(String language) {

13
lib/modules/settings/views/settings_view.dart

@ -11,6 +11,7 @@ class SettingsView extends GetView<SettingsController> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// 直接使用 Theme.of(context) 获取当前主题
final isDarkMode = Theme.of(context).brightness == Brightness.dark; final isDarkMode = Theme.of(context).brightness == Brightness.dark;
return Scaffold( return Scaffold(
@ -237,22 +238,22 @@ class SettingsView extends GetView<SettingsController> {
Divider(height: 1, color: isDarkMode ? Colors.white.withOpacity(0.1) : Colors.grey[200]), Divider(height: 1, color: isDarkMode ? Colors.white.withOpacity(0.1) : Colors.grey[200]),
// 主题设置 // 主题设置
Obx(() => _buildToggleSetting( _buildToggleSetting(
title: '暗黑模式', title: '暗黑模式',
subtitle: '切换应用的明暗主题', subtitle: '切换应用的明暗主题',
icon: controller.isDarkMode.value ? Icons.dark_mode : Icons.light_mode, icon: isDarkMode ? Icons.dark_mode : Icons.light_mode,
iconBgColor: controller.isDarkMode.value iconBgColor: isDarkMode
? Colors.indigo[900]!.withOpacity(0.3) ? Colors.indigo[900]!.withOpacity(0.3)
: Colors.amber[100]!, : Colors.amber[100]!,
iconColor: controller.isDarkMode.value iconColor: isDarkMode
? Colors.indigo[300]! ? Colors.indigo[300]!
: Colors.amber[600]!, : Colors.amber[600]!,
value: controller.isDarkMode.value, value: isDarkMode,
onChanged: (value) { onChanged: (value) {
controller.toggleTheme(); controller.toggleTheme();
}, },
isDarkMode: isDarkMode, isDarkMode: isDarkMode,
)), ),
Divider(height: 1, color: isDarkMode ? Colors.white.withOpacity(0.1) : Colors.grey[200]), Divider(height: 1, color: isDarkMode ? Colors.white.withOpacity(0.1) : Colors.grey[200]),
// 备份与恢复 // 备份与恢复

12
lib/modules/splash/controllers/splash_controller.dart

@ -25,9 +25,6 @@ class SplashController extends GetxController {
// 确保页面先渲染,再执行其他操作 // 确保页面先渲染,再执行其他操作
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
// 检查并应用上次保存的皮肤模式
_checkAndApplyTheme();
// 启动进度条动画 // 启动进度条动画
_startLoadingAnimation(); _startLoadingAnimation();
@ -36,15 +33,6 @@ class SplashController extends GetxController {
}); });
} }
// 检查并应用上次保存的皮肤模式
void _checkAndApplyTheme() {
// 获取保存的主题模式
final savedThemeMode = _themeService.getThemeMode();
// 应用保存的主题模式
Get.changeThemeMode(savedThemeMode);
}
// 进度条动画 // 进度条动画
void _startLoadingAnimation() { void _startLoadingAnimation() {
// 模拟0.5秒的加载进度 // 模拟0.5秒的加载进度

5
lib/modules/translation/controllers/translation_controller.dart

@ -82,6 +82,11 @@ class TranslationController extends GetxController {
// 使用微任务在布局完成后自动滚动到底部 // 使用微任务在布局完成后自动滚动到底部
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToBottom(); _scrollToBottom();
// 额外的延迟滚动,确保在复杂视图中也能正确滚动
Future.delayed(const Duration(milliseconds: 500), () {
_scrollToBottom();
});
}); });
} }

Loading…
Cancel
Save