// 详情页:只读展示 + 收藏切换 + 删除。 import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../application/providers.dart'; import '../../domain/bookmark.dart'; class DetailPage extends ConsumerWidget { const DetailPage({super.key, required this.id}); final int id; @override Widget build(BuildContext context, WidgetRef ref) { final AsyncValue bookmark = ref.watch(bookmarkByIdProvider(id)); return Scaffold( appBar: AppBar( title: const Text('书签详情'), actions: [ IconButton( key: const Key('toggle-favorite'), onPressed: () { final Bookmark? b = bookmark.value; if (b != null) ref.read(bookmarkActionsProvider).toggleFavorite(b); }, icon: Icon(bookmark.value?.favorite ?? false ? Icons.star : Icons.star_border), ), ], ), body: bookmark.when( loading: () => const Center(child: CircularProgressIndicator()), error: (Object e, StackTrace _) => Center(child: Text('读取失败:$e')), data: (Bookmark? b) { if (b == null) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('书签不存在(可能已被删除)', key: Key('missing')), const SizedBox(height: 12), FilledButton(onPressed: () => context.pop(), child: const Text('返回')), ], ), ); } return ListView( padding: const EdgeInsets.all(16), children: [ Text(b.title, key: const Key('detail-title'), style: Theme.of(context).textTheme.headlineSmall), const SizedBox(height: 8), Text(b.url, key: const Key('detail-url'), style: TextStyle(color: Theme.of(context).colorScheme.primary)), const SizedBox(height: 8), Text('创建于 ${b.createdAt.toLocal()}'), const Divider(height: 32), Text(b.note.isEmpty ? '(没有备注)' : b.note, key: const Key('detail-note')), const SizedBox(height: 24), FilledButton.icon( key: const Key('delete'), onPressed: () async { final bool? ok = await showDialog( context: context, builder: (BuildContext ctx) => AlertDialog( title: const Text('删除这条书签?'), actions: [ TextButton(onPressed: () => Navigator.of(ctx).pop(false), child: const Text('取消')), FilledButton( key: const Key('confirm-delete'), onPressed: () => Navigator.of(ctx).pop(true), child: const Text('删除'), ), ], ), ); if (ok != true || !context.mounted) return; await ref.read(bookmarkActionsProvider).delete(b.id); if (!context.mounted) return; context.pop(); }, icon: const Icon(Icons.delete_outline), label: const Text('删除'), ), ], ); }, ), ); } }