Skip to content

Commit

Permalink
# 功能更新
Browse files Browse the repository at this point in the history
- 添加主题颜色选择
# bug修复
- 修复图片加载的部分逻辑问题
  • Loading branch information
deretame committed Jan 18, 2025
1 parent e8f9762 commit 38bf28a
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 153 deletions.
1 change: 0 additions & 1 deletion lib/config/color_theme_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ColorThemeInfo {
}
}

// 将 materialColors 封装到 ColorThemeInfo 中
final List<ColorThemeInfo> colorThemeList = [
ColorThemeInfo(Colors.red, '红色', 0),
ColorThemeInfo(Colors.pink, '粉色', 1),
Expand Down
46 changes: 22 additions & 24 deletions lib/network/http/picture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ Future<String> getCachePicture({
String downloadPath = await getDownloadPath();

// 构建文件路径
String filePath = buildFilePath(
String cacheFilePath = buildFilePath(
cachePath, from, pictureType, cartoonId, chapterId, sanitizedPath);

String downloadFilePath = buildFilePath(
downloadPath, from, pictureType, cartoonId, chapterId, sanitizedPath);

// 检查文件是否存在
String existingFilePath =
await checkFileExists(cachePath, downloadPath, filePath);
await checkFileExists(cacheFilePath, downloadFilePath);
if (existingFilePath.isNotEmpty) {
return existingFilePath;
}
Expand All @@ -47,9 +50,9 @@ Future<String> getCachePicture({
Uint8List imageData = await downloadImageWithRetry(finalUrl);

// 保存图片
await saveImage(imageData, filePath);
await saveImage(imageData, cacheFilePath);

return filePath;
return cacheFilePath;
}

Future<String> downloadPicture({
Expand All @@ -71,21 +74,19 @@ Future<String> downloadPicture({
String downloadPath = await getDownloadPath();
String cachePath = await getCachePath();

// 构建文件路径
String filePath = buildFilePath(
String cacheFilePath = buildFilePath(
cachePath, from, pictureType, cartoonId, chapterId, sanitizedPath);

String downloadFilePath = buildFilePath(
downloadPath, from, pictureType, cartoonId, chapterId, sanitizedPath);

// 检查文件是否存在
if (await fileExists(filePath)) {
return filePath;
}
String existingFilePath =
await checkFileExists(cacheFilePath, downloadFilePath);

// 检查缓存目录是否存在文件
String cacheFilePath = buildFilePath(
cachePath, from, pictureType, cartoonId, chapterId, sanitizedPath);
if (await fileExists(cacheFilePath)) {
await copyFile(cacheFilePath, filePath);
return filePath;
if (existingFilePath.isNotEmpty && existingFilePath != downloadFilePath) {
await copyFile(cacheFilePath, downloadFilePath);
return downloadFilePath;
}

// 处理 URL
Expand All @@ -96,9 +97,9 @@ Future<String> downloadPicture({
Uint8List imageData = await downloadImageWithRetry(finalUrl);

// 保存图片
await saveImage(imageData, filePath);
await saveImage(imageData, downloadFilePath);

return filePath;
return downloadFilePath;
}

String sanitizePath(String path) {
Expand Down Expand Up @@ -127,16 +128,13 @@ String buildFilePath(
Future<String> checkFileExists(
String cachePath,
String downloadPath,
String filePath,
) async {
if (await fileExists(filePath)) {
return filePath;
if (await fileExists(cachePath)) {
return cachePath;
}

String downloadFilePath = file_path.join(
downloadPath, file_path.relative(filePath, from: cachePath));
if (await fileExists(downloadFilePath)) {
return downloadFilePath;
if (await fileExists(downloadPath)) {
return downloadPath;
}

return '';
Expand Down
29 changes: 29 additions & 0 deletions lib/page/setting/view/global_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class _GlobalSettingPageState extends State<GlobalSettingPage> {
children: [
_systemTheme(),
_dynamicColor(),
if (!globalSetting.dynamicColor) ...[
SizedBox(height: 11),
changeThemeColor(context),
SizedBox(height: 11),
],
_isAMOLED(),
if (kDebugMode) ...[
ElevatedButton(
Expand Down Expand Up @@ -142,6 +147,30 @@ class _GlobalSettingPageState extends State<GlobalSettingPage> {
globalSetting.setDynamicColor(_dynamicColorValue);
}

Widget changeThemeColor(BuildContext context) {
final router = AutoRouter.of(context);
return GestureDetector(
onTap: () async {
router.push(ThemeColorRoute());
},
behavior: HitTestBehavior.opaque, // 使得所有透明区域也可以响应点击
child: Row(
children: [
SizedBox(width: 10),
Text(
"主题颜色",
style: TextStyle(
fontSize: 18,
),
),
Expanded(child: Container()),
Icon(Icons.chevron_right),
SizedBox(width: 10),
],
),
);
}

Widget _isAMOLED() {
return Row(
children: [
Expand Down
2 changes: 2 additions & 0 deletions lib/page/theme_color/theme_color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export './view/view.dart';
export './widgets/widgets.dart';
66 changes: 66 additions & 0 deletions lib/page/theme_color/view/theme_color_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:auto_route/annotations.dart';
import 'package:flutter/material.dart';
import 'package:zephyr/config/color_theme_types.dart';
import 'package:zephyr/main.dart';
import 'package:zephyr/page/theme_color/theme_color.dart';

@RoutePage()
class ThemeColorPage extends StatefulWidget {
const ThemeColorPage({super.key});

@override
State<ThemeColorPage> createState() => _ThemeColorPageState();
}

class _ThemeColorPageState extends State<ThemeColorPage> {
Color _currentColor = globalSetting.seedColor; // 当前选择的颜色

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('主题颜色'),
),
body: SingleChildScrollView(
child: Column(
children: [
// 颜色选择器
ColorPickerPage(
currentColor: _currentColor,
onColorChanged: (color) {
setState(() {
_currentColor = color; // 更新颜色状态
});
_setThemeColor(color); // 更新全局主题颜色
},
),
// 颜色块网格
Padding(
padding: const EdgeInsets.all(16.0),
child: Wrap(
spacing: 16.0, // 水平间距
runSpacing: 16.0, // 垂直间距
children: colorThemeList.map((colorInfo) {
return ColorThemeItem(
colorInfo: colorInfo,
currentColor: _currentColor,
onColorSelected: (color) {
setState(() {
_currentColor = color; // 更新颜色状态
});
_setThemeColor(color); // 更新全局主题颜色
},
);
}).toList(),
),
),
],
),
),
);
}

void _setThemeColor(Color color) {
globalSetting.setSeedColor(color);
}
}
1 change: 1 addition & 0 deletions lib/page/theme_color/view/view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export './theme_color_page.dart';
54 changes: 54 additions & 0 deletions lib/page/theme_color/widgets/color_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:zephyr/config/global.dart';

import '../../../config/color_theme_types.dart';

class ColorThemeItem extends StatelessWidget {
final ColorThemeInfo colorInfo;
final Color currentColor;
final Function(Color) onColorSelected;

const ColorThemeItem({
super.key,
required this.colorInfo,
required this.currentColor,
required this.onColorSelected,
});

@override
Widget build(BuildContext context) {
return SizedBox(
width: screenWidth / 4 - 30, // 每个颜色块的宽度
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
onColorSelected(colorInfo.color); // 点击颜色块后回调
},
borderRadius: BorderRadius.circular(8),
child: Column(
children: [
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: colorInfo.color,
borderRadius: BorderRadius.circular(8), // 圆角
border: currentColor == colorInfo.color
? Border.all(color: Colors.black, width: 2) // 选中状态
: null,
),
),
SizedBox(height: 8), // 颜色块和文字的间距
Text(
colorInfo.label,
style: TextStyle(fontSize: 14),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}
Empty file.
30 changes: 30 additions & 0 deletions lib/page/theme_color/widgets/ring_color_pick.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

class ColorPickerPage extends StatelessWidget {
final Color currentColor;
final Function(Color) onColorChanged;

const ColorPickerPage({
super.key,
required this.currentColor,
required this.onColorChanged,
});

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
// 颜色选择器
HueRingPicker(
pickerColor: currentColor,
onColorChanged: onColorChanged, // 颜色变化时回调
displayThumbColor: true, // 是否显示拇指颜色
),
],
),
);
}
}
3 changes: 3 additions & 0 deletions lib/page/theme_color/widgets/widgets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export './color_box.dart';
export './color_grid.dart';
export './ring_color_pick.dart';
1 change: 1 addition & 0 deletions lib/util/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AppRouter extends RootStackRouter {
AutoRoute(page: UserCommentsRoute.page),
AutoRoute(page: BikaSettingRoute.page),
AutoRoute(page: GlobalSettingRoute.page),
AutoRoute(page: ThemeColorRoute.page),
AutoRoute(page: ShowColorRoute.page),
];

Expand Down
Loading

0 comments on commit 38bf28a

Please sign in to comment.