195 lines
7.0 KiB
Dart
195 lines
7.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../../core/config/app_config.dart';
|
|
import '../../features/cps/cps_provider.dart';
|
|
import '../../shared/widgets/empty_view.dart';
|
|
import '../../shared/widgets/error_view.dart';
|
|
import '../../shared/widgets/loading_view.dart';
|
|
import 'wardrobe_provider.dart';
|
|
|
|
/// 衣橱:服装网格 + 长按删除 + 上传入口
|
|
class WardrobePage extends ConsumerStatefulWidget {
|
|
const WardrobePage({super.key});
|
|
|
|
@override
|
|
ConsumerState<WardrobePage> createState() => _WardrobePageState();
|
|
}
|
|
|
|
class _WardrobePageState extends ConsumerState<WardrobePage> {
|
|
String? _filter; // 当前分类筛选,null = 全部
|
|
|
|
/// 长按菜单:找升级款 / 删除
|
|
Future<void> _showItemMenu(WardrobeItemInfo item) async {
|
|
final action = await showModalBottomSheet<String>(
|
|
context: context,
|
|
builder: (ctx) => SafeArea(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
ListTile(
|
|
leading: const Icon(Icons.upgrade_outlined),
|
|
title: const Text('找升级款'),
|
|
onTap: () => Navigator.pop(ctx, 'upgrade'),
|
|
),
|
|
ListTile(
|
|
leading: const Icon(Icons.delete_outline),
|
|
title: const Text('删除'),
|
|
onTap: () => Navigator.pop(ctx, 'delete'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
if (action == 'upgrade') {
|
|
await _openUpgrade(item);
|
|
} else if (action == 'delete') {
|
|
await _confirmDelete(item);
|
|
}
|
|
}
|
|
|
|
/// 升级款推荐:空结果提示后不跳转
|
|
Future<void> _openUpgrade(WardrobeItemInfo item) async {
|
|
final rec = await ref.read(cpsUpgradeProvider(item.id).future);
|
|
if (!mounted) return;
|
|
if (rec.isEmpty) {
|
|
Fluttertoast.showToast(msg: '暂无升级款');
|
|
return;
|
|
}
|
|
final first = rec.first;
|
|
context.push('/cps-product-list', extra: CpsListArgs(
|
|
title: '${item.category}升级款',
|
|
source: first.source,
|
|
categoryCode: first.categoryCode,
|
|
city: first.city,
|
|
scene: 'wardrobe_upgrade',
|
|
));
|
|
}
|
|
|
|
Future<void> _confirmDelete(WardrobeItemInfo item) async {
|
|
final ok = await showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('删除这件服装?'),
|
|
content: Text('「${item.category}」删除后不可恢复'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx, false),
|
|
child: const Text('取消')),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(ctx, true),
|
|
child: const Text('删除')),
|
|
],
|
|
),
|
|
);
|
|
if (ok == true) {
|
|
await ref.read(wardrobeProvider.notifier).delete(item.id);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final items = ref.watch(wardrobeProvider);
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('我的衣橱')),
|
|
body: Column(
|
|
children: [
|
|
SizedBox(
|
|
height: 48,
|
|
child: ListView(
|
|
scrollDirection: Axis.horizontal,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
children: [
|
|
for (final c in [null, ...wardrobeCategories])
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: ChoiceChip(
|
|
label: Text(c ?? '全部'),
|
|
selected: _filter == c,
|
|
onSelected: (_) => setState(() => _filter = c),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: items.when(
|
|
loading: () => const LoadingView(text: '加载衣橱...'),
|
|
error: (e, _) => ErrorView(
|
|
message: e.toString().replaceFirst('Exception: ', ''),
|
|
onRetry: () => ref.read(wardrobeProvider.notifier).refresh(),
|
|
),
|
|
data: (list) {
|
|
final shown = _filter == null
|
|
? list
|
|
: list.where((i) => i.category == _filter).toList();
|
|
if (shown.isEmpty) {
|
|
return EmptyView(
|
|
message: '衣橱还是空的,上传你的服装吧',
|
|
actionText: '上传服装',
|
|
onAction: () => context.go('/wardrobe-upload'));
|
|
}
|
|
return GridView.builder(
|
|
padding: const EdgeInsets.all(12),
|
|
gridDelegate:
|
|
const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 2,
|
|
mainAxisSpacing: 12,
|
|
crossAxisSpacing: 12,
|
|
childAspectRatio: 0.8,
|
|
),
|
|
itemCount: shown.length,
|
|
itemBuilder: (ctx, i) {
|
|
final item = shown[i];
|
|
return GestureDetector(
|
|
onLongPress: () => _showItemMenu(item),
|
|
child: Card(
|
|
clipBehavior: Clip.antiAlias,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Expanded(
|
|
child: item.photoUrl.isEmpty
|
|
? const Icon(Icons.checkroom,
|
|
size: 48, color: Colors.grey)
|
|
: Image.network(
|
|
AppConfig.resolveUrl(item.photoUrl),
|
|
fit: BoxFit.cover,
|
|
errorBuilder: (_, _, _) => const Icon(
|
|
Icons.broken_image_outlined,
|
|
size: 48,
|
|
color: Colors.grey),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8),
|
|
child: Text(
|
|
'${item.category}${item.season.isNotEmpty ? ' · ${item.season}' : ''}',
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
fontSize: 13, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
onPressed: () => context.go('/wardrobe-upload'),
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('上传'),
|
|
),
|
|
);
|
|
}
|
|
}
|