Files
ttstd_family_care/lib/features/home/presentation/screenshot_page.dart
T
tongtong 71dd8eb52c feat(theme): 添加全局深色模式支持与主题适配
- 引入 DarkMode 工具类,提供系统栏、颜色、色调的暗色适配能力
- AppColors 调色板改为 CupertinoDynamicColor.withBrightness 自适应
- app.dart 增加 AnnotatedRegion 与 Material Theme 包裹以联动暗色系统栏
- 移除 system_ui_util 中硬编码的浅色图标配置,改由主题驱动
- CardBox、AppDivider、SectionTitle 等公共组件适配深色卡片与分割线
- settings_ui、change_mobile、change_password、login_devices、wechat_binding 等页面全面接入 DarkMode 颜色
- dio_client 新增 allowStale 响应缓存机制,网络异常时回退本地缓存
- token_storage 增加 currentAccountId(JWT 解析)及按用户隔离的设备偏好存取
- ApiException 改为通过私有构造持有真实 AppError 实例,appError getter 返回结构化错误
- 闹钟模块迁移至 FutureProvider.autoDispose.family,消除手动 loading 状态管理
- 闹钟编辑页 CupertinoTimerPicker 替换为 CupertinoDatePicker(24h 格式),标签输入改为 TextEditingController
2026-11-07 02:35:30 +08:00

343 lines
11 KiB
Dart

import 'package:ttstd_family_care/core/network/cached_network_image.dart';
import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:material_ui/material_ui.dart';
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
import '../../../l10n/app_localizations.dart';
import '../../device/data/device_providers.dart';
import '../../device/domain/device_models.dart';
class ScreenshotPage extends ConsumerStatefulWidget {
const ScreenshotPage({
super.key,
required this.screenshots,
required this.sn,
this.initialIndex = 0,
});
final List<ScreenshotVO> screenshots;
final String sn;
final int initialIndex;
@override
ConsumerState<ScreenshotPage> createState() => _ScreenshotPageState();
}
class _ScreenshotPageState extends ConsumerState<ScreenshotPage> {
late final PageController _pageController;
late List<ScreenshotVO> _screenshots;
late int _currentIndex;
@override
void initState() {
super.initState();
_screenshots = List<ScreenshotVO>.from(widget.screenshots);
_currentIndex = widget.initialIndex.clamp(0, _screenshots.length - 1);
_pageController = PageController(initialPage: _currentIndex);
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
/// 刷新截图列表:本地移除后立即失效源 Provider,
/// 返回首页时最近截图列表会自动重新拉取。
Future<void> _refreshSource() async {
ref.invalidate(recentScreenshotsProvider(widget.sn));
}
void _deleteCurrent() {
final l10n = AppLocalizations.of(context);
showCupertinoDialog<bool>(
context: context,
builder: (dialogContext) => CupertinoAlertDialog(
title: Text(l10n.homeScreenshotDeleteCurrent),
content: Text(l10n.homeScreenshotDeleteConfirm),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.of(dialogContext).pop(false),
child: Text(l10n.cancel),
),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () => Navigator.of(dialogContext).pop(true),
child: Text(l10n.homeScreenshotDelete),
),
],
),
).then((confirmed) async {
if (confirmed != true) return;
final target = _screenshots[_currentIndex];
final id = target.id;
if (id == null) return;
// 先调用后台 client 接口删除单条截图。
final err = await ref
.read(deviceRepositoryProvider)
.deleteScreenshot(widget.sn, id);
if (!mounted) return;
if (err != null) {
_showError(l10n, err);
return;
}
setState(() {
_screenshots.removeAt(_currentIndex);
if (_currentIndex > 0) _currentIndex -= 1;
});
_refreshSource();
if (_screenshots.isEmpty) {
if (mounted) Navigator.of(context).pop();
return;
}
final safeIndex = _currentIndex.clamp(0, _screenshots.length - 1);
if (safeIndex != _pageController.page?.round()) {
_pageController.jumpToPage(safeIndex);
}
if (mounted) _toast(l10n.homeScreenshotDeleted);
});
}
void _clearAll() {
final l10n = AppLocalizations.of(context);
showCupertinoDialog<bool>(
context: context,
builder: (dialogContext) => CupertinoAlertDialog(
title: Text(l10n.homeScreenshotClearAll),
content: Text(l10n.homeScreenshotClearConfirm),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.of(dialogContext).pop(false),
child: Text(l10n.cancel),
),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () => Navigator.of(dialogContext).pop(true),
child: Text(l10n.homeScreenshotClearAll),
),
],
),
).then((confirmed) async {
if (confirmed != true) return;
if (_screenshots.isEmpty) return;
// 先调用后台 client 接口清空全部截图。
final err = await ref
.read(deviceRepositoryProvider)
.clearScreenshots(widget.sn);
if (!mounted) return;
if (err != null) {
_showError(l10n, err);
return;
}
setState(() {
_screenshots.clear();
});
_refreshSource();
if (mounted) {
Navigator.of(context).pop();
_toast(l10n.homeScreenshotCleared);
}
});
}
void _showError(AppLocalizations l10n, String message) {
if (!mounted) return;
showCupertinoDialog<void>(
context: context,
builder: (dialogContext) => CupertinoAlertDialog(
content: Text(message),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.of(dialogContext).pop(),
child: Text(l10n.commonConfirm),
),
],
),
);
}
void _toast(String message) {
final overlay = Overlay.of(context);
if (overlay == null) return;
final entry = OverlayEntry(
builder: (ctx) => Positioned(
bottom: 90,
left: 24,
right: 24,
child: IgnorePointer(
child: Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: CupertinoColors.systemGrey.withValues(alpha: 0.92),
borderRadius: BorderRadius.circular(8),
),
child: Text(
message,
style: const TextStyle(color: CupertinoColors.white),
textAlign: TextAlign.center,
),
),
),
),
),
);
overlay.insert(entry);
Future.delayed(const Duration(seconds: 2), () => entry.remove());
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
return AppPageScaffold(
backgroundColor: CupertinoColors.black,
navigationBar: CupertinoNavigationBar(
backgroundColor: CupertinoColors.black.withValues(alpha: 0.85),
brightness: Brightness.dark,
middle: _screenshots.isEmpty
? null
: Text(
'${_currentIndex + 1} / ${_screenshots.length}',
style: const TextStyle(color: CupertinoColors.white),
),
leading: CupertinoNavigationBarBackButton(
color: CupertinoColors.white,
onPressed: () => Navigator.of(context).pop(),
),
trailing: _screenshots.isNotEmpty
? CupertinoButton(
padding: EdgeInsets.zero,
onPressed: _clearAll,
child: Text(
l10n.homeScreenshotClearAll,
style: const TextStyle(
color: CupertinoColors.white,
fontSize: 15,
),
),
)
: null,
),
child: SafeArea(
child: Column(
children: [
Expanded(
child: _screenshots.isEmpty
? Center(
child: Text(
l10n.homeScreenshotEmpty,
style:
const TextStyle(color: CupertinoColors.systemGrey),
),
)
: PageView.builder(
controller: _pageController,
itemCount: _screenshots.length,
onPageChanged: (index) {
setState(() {
_currentIndex = index;
});
},
itemBuilder: (context, index) {
final item = _screenshots[index];
final url = item.url;
return InteractiveViewer(
child: Center(
child: url != null
? Image(
image: cachedNetworkImage(url),
fit: BoxFit.contain,
errorBuilder: (ctx, error, stack) => Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
CupertinoIcons.photo,
size: 64,
color: CupertinoColors.systemGrey,
),
const SizedBox(height: 8),
Text(
l10n.commonLoadFailed,
style: const TextStyle(
color:
CupertinoColors.systemGrey,
),
),
],
),
)
: const Icon(
CupertinoIcons.photo,
size: 64,
color: CupertinoColors.systemGrey,
),
),
);
},
),
),
if (_screenshots.isNotEmpty)
SafeArea(
top: false,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
decoration: BoxDecoration(
color: CupertinoColors.black.withValues(alpha: 0.85),
border: Border(
top: BorderSide(
color: CupertinoColors.separator
.withValues(alpha: 0.3),
width: 0.5,
),
),
),
child: CupertinoButton(
padding: const EdgeInsets.symmetric(vertical: 12),
color: CupertinoColors.destructiveRed,
borderRadius: BorderRadius.circular(10),
onPressed: _deleteCurrent,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
CupertinoIcons.delete,
color: CupertinoColors.white,
size: 20,
),
const SizedBox(width: 8),
Text(
l10n.homeScreenshotDelete,
style: const TextStyle(
color: CupertinoColors.white,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
],
),
),
),
),
],
),
),
);
}
}