Skip to content

Commit fb74848

Browse files
committed
feat: 新增添加背景图片设置项
1 parent f2188e1 commit fb74848

File tree

7 files changed

+422
-2
lines changed

7 files changed

+422
-2
lines changed

lib/main.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'dart:async';
2-
import 'dart:io' show Platform;
2+
import 'dart:io' show Platform, File;
33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_localizations/flutter_localizations.dart';
@@ -181,6 +181,10 @@ class _HomeworkBoardState extends State<HomeworkBoard> with WindowListener, Tick
181181
// 背景不透明度
182182
double _backgroundOpacity = 1.0;
183183

184+
// 背景图片相关
185+
String? _backgroundImagePath;
186+
int _backgroundImageMode = 0;
187+
184188
// 快捷菜单动画控制器
185189
late AnimationController _quickMenuAnimationController;
186190
late Animation<double> _quickMenuOpacityAnimation;
@@ -299,6 +303,8 @@ class _HomeworkBoardState extends State<HomeworkBoard> with WindowListener, Tick
299303
final settingsService = SettingsService.instance;
300304
setState(() {
301305
_backgroundOpacity = settingsService.getBackgroundOpacity();
306+
_backgroundImagePath = settingsService.getBackgroundImagePath();
307+
_backgroundImageMode = settingsService.getBackgroundImageMode();
302308
});
303309
}
304310

@@ -669,6 +675,13 @@ class _HomeworkBoardState extends State<HomeworkBoard> with WindowListener, Tick
669675
color: _isFullScreen
670676
? Theme.of(context).colorScheme.surface
671677
: Theme.of(context).colorScheme.surface.withValues(alpha: _backgroundOpacity),
678+
image: _backgroundImagePath != null && _backgroundImagePath!.isNotEmpty
679+
? DecorationImage(
680+
image: FileImage(File(_backgroundImagePath!)),
681+
fit: _getBoxFitFromMode(_backgroundImageMode),
682+
opacity: 0.3, // 半透明显示
683+
)
684+
: null,
672685
borderRadius: _isFullScreen
673686
? BorderRadius.zero
674687
: BorderRadius.circular(12),
@@ -1429,4 +1442,17 @@ class _HomeworkBoardState extends State<HomeworkBoard> with WindowListener, Tick
14291442
// 保存失败,静默处理
14301443
}
14311444
}
1445+
1446+
BoxFit _getBoxFitFromMode(int mode) {
1447+
switch (mode) {
1448+
case 0:
1449+
return BoxFit.contain; // 适应
1450+
case 1:
1451+
return BoxFit.cover; // 填充
1452+
case 2:
1453+
return BoxFit.fill; // 拉伸
1454+
default:
1455+
return BoxFit.contain;
1456+
}
1457+
}
14321458
}

lib/models/app_config.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class AppConfig {
1010
final bool firstLaunch; // 是否首次启动
1111
final bool showInTaskbar; // 是否在任务栏显示
1212
final int? themeColor; // 自定义主题色,null表示使用默认色
13+
final String? backgroundImagePath; // 背景图片路径
14+
final int backgroundImageMode; // 背景图片显示模式: 0=适应, 1=填充, 2=拉伸
1315

1416
const AppConfig({
1517
this.theme = 'light',
@@ -23,6 +25,8 @@ class AppConfig {
2325
this.firstLaunch = true,
2426
this.showInTaskbar = false,
2527
this.themeColor,
28+
this.backgroundImagePath,
29+
this.backgroundImageMode = 0,
2630
});
2731

2832
AppConfig copyWith({
@@ -37,6 +41,8 @@ class AppConfig {
3741
bool? firstLaunch,
3842
bool? showInTaskbar,
3943
int? themeColor,
44+
String? backgroundImagePath,
45+
int? backgroundImageMode,
4046
}) {
4147
return AppConfig(
4248
theme: theme ?? this.theme,
@@ -50,6 +56,8 @@ class AppConfig {
5056
firstLaunch: firstLaunch ?? this.firstLaunch,
5157
showInTaskbar: showInTaskbar ?? this.showInTaskbar,
5258
themeColor: themeColor ?? this.themeColor,
59+
backgroundImagePath: backgroundImagePath ?? this.backgroundImagePath,
60+
backgroundImageMode: backgroundImageMode ?? this.backgroundImageMode,
5361
);
5462
}
5563

@@ -67,6 +75,8 @@ class AppConfig {
6775
'firstLaunch': firstLaunch,
6876
'showInTaskbar': showInTaskbar,
6977
'themeColor': themeColor,
78+
'backgroundImagePath': backgroundImagePath,
79+
'backgroundImageMode': backgroundImageMode,
7080
};
7181
}
7282

@@ -84,6 +94,8 @@ class AppConfig {
8494
firstLaunch: json['firstLaunch'] as bool? ?? true,
8595
showInTaskbar: json['showInTaskbar'] as bool? ?? false,
8696
themeColor: json['themeColor'] as int?,
97+
backgroundImagePath: json['backgroundImagePath'] as String?,
98+
backgroundImageMode: json['backgroundImageMode'] as int? ?? 0,
8799
);
88100
}
89101
}

lib/services/settings_service.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,48 @@ class SettingsService {
438438
}
439439
}
440440

441+
/// 获取背景图片路径
442+
String? getBackgroundImagePath() {
443+
final config = JsonStorageService.instance.getAppConfig();
444+
return config.backgroundImagePath;
445+
}
446+
447+
/// 设置背景图片路径
448+
Future<bool> setBackgroundImagePath(String? path) async {
449+
try {
450+
// 更新JSON配置
451+
final storageService = JsonStorageService.instance;
452+
final currentConfig = storageService.getAppConfig();
453+
final updatedConfig = currentConfig.copyWith(
454+
backgroundImagePath: path,
455+
);
456+
await storageService.saveAppConfig(updatedConfig);
457+
return true;
458+
} catch (e) {
459+
return false;
460+
}
461+
}
462+
463+
/// 获取背景图片显示模式
464+
int getBackgroundImageMode() {
465+
final config = JsonStorageService.instance.getAppConfig();
466+
return config.backgroundImageMode;
467+
}
468+
469+
/// 设置背景图片显示模式: 0=适应, 1=填充, 2=拉伸
470+
Future<bool> setBackgroundImageMode(int mode) async {
471+
try {
472+
// 更新JSON配置
473+
final storageService = JsonStorageService.instance;
474+
final currentConfig = storageService.getAppConfig();
475+
final updatedConfig = currentConfig.copyWith(
476+
backgroundImageMode: mode,
477+
);
478+
await storageService.saveAppConfig(updatedConfig);
479+
return true;
480+
} catch (e) {
481+
return false;
482+
}
483+
}
484+
441485
}

0 commit comments

Comments
 (0)