generated from dellevin/template
优化代码结构,修复部分bug
This commit is contained in:
@@ -960,7 +960,7 @@ class _GameDetailPageState extends State<GameDetailPage> {
|
||||
onPressed: () async {
|
||||
final provider = context.read<AppProvider>();
|
||||
await provider.removeGame(widget.game.id);
|
||||
if (!mounted) return;
|
||||
if (!mounted || !context.mounted) return;
|
||||
if (widget.embedded) {
|
||||
Navigator.of(context).pop();
|
||||
provider.selectGame(null);
|
||||
@@ -969,7 +969,7 @@ class _GameDetailPageState extends State<GameDetailPage> {
|
||||
navigator.pop();
|
||||
navigator.pop();
|
||||
}
|
||||
if (mounted) {
|
||||
if (mounted && context.mounted) {
|
||||
ToastUtil.show(context, '已删除');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
@@ -185,6 +186,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
initialValue: _titleController.text,
|
||||
hint: '请输入游戏名称',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _titleController.text = result);
|
||||
},
|
||||
),
|
||||
@@ -210,6 +212,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
initialSelected: _platforms,
|
||||
hint: '如:PS5、Switch、Steam',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _platforms = result);
|
||||
},
|
||||
),
|
||||
@@ -235,6 +238,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
initialSelected: _versions,
|
||||
hint: '如:标准版、豪华版',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _versions = result);
|
||||
},
|
||||
),
|
||||
@@ -269,6 +273,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
initialSelected: _genres,
|
||||
hint: '如:RPG、动作、冒险',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _genres = result);
|
||||
},
|
||||
),
|
||||
@@ -305,6 +310,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
initialSelected: _purchasePlatforms,
|
||||
hint: '如:Steam、eShop、PlayStation Store',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _purchasePlatforms = result);
|
||||
},
|
||||
),
|
||||
@@ -344,6 +350,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
hint: '如:298元、49.99美元',
|
||||
keyboardType: TextInputType.text,
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) setState(() => _purchasePriceController.text = result);
|
||||
},
|
||||
),
|
||||
@@ -496,6 +503,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
builder: (_) => _SummaryEditorPage(initialText: _summaryController.text),
|
||||
),
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) {
|
||||
setState(() => _summaryController.text = result);
|
||||
}
|
||||
@@ -753,10 +761,11 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
|
||||
if (pickedFile != null) {
|
||||
final fileName = 'cover_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
final gameId = widget.game?.id ?? DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final gameId = widget.game?.id ?? const Uuid().v4();
|
||||
final targetPath = await ImagePathHelper.instance.getGameCoverPath(gameId, fileName);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
await File(pickedFile.path).copy(targetPath);
|
||||
if (!mounted) return;
|
||||
setState(() => _coverPath = targetPath);
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -919,11 +928,12 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
if (response.bodyBytes.length > 10 * 1024 * 1024) throw Exception('图片太大');
|
||||
|
||||
final fileName = 'cover_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
final gameId = widget.game?.id ?? DateTime.now().millisecondsSinceEpoch.toString();
|
||||
final gameId = widget.game?.id ?? const Uuid().v4();
|
||||
final targetPath = await ImagePathHelper.instance.getGameCoverPath(gameId, fileName);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
await File(targetPath).writeAsBytes(response.bodyBytes);
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() => _coverPath = targetPath);
|
||||
} catch (e) {
|
||||
debugPrint('封面下载失败: $e');
|
||||
@@ -1009,6 +1019,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
lastDate: DateTime.now().add(const Duration(days: 365 * 5)),
|
||||
builder: (context, child) => child!,
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (picked != null) {
|
||||
setState(() => _purchaseDate = picked);
|
||||
}
|
||||
@@ -1073,7 +1084,7 @@ class _GameFormPageState extends State<GameFormPage> {
|
||||
final now = DateTime.now();
|
||||
|
||||
if (widget.game == null) {
|
||||
final newGameId = now.millisecondsSinceEpoch.toString();
|
||||
final newGameId = const Uuid().v4();
|
||||
String? finalCoverPath;
|
||||
if (_coverPath != null && _coverPath!.isNotEmpty) {
|
||||
finalCoverPath = await _moveCoverToNewId(_coverPath!, newGameId);
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
|
||||
@@ -235,7 +236,7 @@ class _GameReviewFormPageState extends State<GameReviewFormPage> {
|
||||
final now = DateTime.now();
|
||||
if (widget.review == null) {
|
||||
final newReview = GameReview(
|
||||
id: now.millisecondsSinceEpoch.toString(),
|
||||
id: const Uuid().v4(),
|
||||
gameId: widget.gameId,
|
||||
content: _contentController.text.trim(),
|
||||
reviewer: _reviewerController.text.trim(),
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../widgets/fade_in_local_image.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
@@ -230,7 +231,7 @@ class _GameScreenshotsPageState extends State<GameScreenshotsPage> {
|
||||
await File(pickedFile.path).copy(targetPath);
|
||||
|
||||
final newScreenshot = GameScreenshot(
|
||||
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
id: const Uuid().v4(),
|
||||
gameId: widget.game.id,
|
||||
screenshotPath: targetPath,
|
||||
createdAt: DateTime.now(),
|
||||
@@ -317,7 +318,7 @@ class _GameScreenshotsPageState extends State<GameScreenshotsPage> {
|
||||
await File(targetPath).writeAsBytes(response.bodyBytes);
|
||||
|
||||
final newScreenshot = GameScreenshot(
|
||||
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
id: const Uuid().v4(),
|
||||
gameId: widget.game.id,
|
||||
screenshotPath: targetPath,
|
||||
createdAt: DateTime.now(),
|
||||
|
||||
@@ -446,8 +446,9 @@ class _GameTabPageState extends State<GameTabPage> {
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
await context.read<AppProvider>().removeGame(game.id);
|
||||
if (!ctx.mounted) return;
|
||||
Navigator.pop(ctx);
|
||||
_loadFirst();
|
||||
if (mounted) _loadFirst();
|
||||
},
|
||||
style: ElevatedButton.styleFrom(backgroundColor: colors.error, foregroundColor: colors.onError, elevation: 0,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
|
||||
Reference in New Issue
Block a user