import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:japp_flutter/core/models/challenge_model.dart'; import 'package:japp_flutter/features/challenge/view_models/challenge_edit_vm.dart'; class ChallengeEditScreen extends ConsumerWidget { final ChallengeModel? initialChallenge; const ChallengeEditScreen({ super.key, this.initialChallenge, }); @override Widget build(BuildContext context, WidgetRef ref) { final challengeState = ref.watch(challengeEditProvider); final theme = Theme.of(context); return Scaffold( appBar: AppBar( title: Text(initialChallenge == null ? '创建挑战' : '编辑挑战'), actions: [ IconButton( icon: const Icon(Icons.save), onPressed: () async { await ref.read(challengeEditProvider.notifier).saveChallenge(); if (context.mounted) Navigator.pop(context); }, ), ], ), body: challengeState.when( loading: () => const Center(child: CircularProgressIndicator()), error: (error, _) => Center(child: Text('错误: $error')), data: (challenge) { if (challenge == null) { return const Center(child: Text('未初始化数据')); } return SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildTitleField(challenge, ref), const SizedBox(height: 24), _buildDifficultySelector(challenge, ref), const SizedBox(height: 24), _buildDescriptionField(challenge, ref), ], ), ); }, ), ); } Widget _buildTitleField(ChallengeModel challenge, WidgetRef ref) { return TextFormField( initialValue: challenge.title, decoration: const InputDecoration( labelText: '挑战标题', border: OutlineInputBorder(), ), onChanged: (value) => ref.read(challengeEditProvider.notifier).updateTitle(value), ); } Widget _buildDescriptionField(ChallengeModel challenge, WidgetRef ref) { return TextFormField( initialValue: challenge.description, decoration: const InputDecoration( labelText: '挑战描述', border: OutlineInputBorder(), alignLabelWithHint: true, ), maxLines: 5, onChanged: (value) => ref.read(challengeEditProvider.notifier).updateDescription(value), ); } Widget _buildDifficultySelector(ChallengeModel challenge, WidgetRef ref) { // const difficulties = ['简单', '中等', '困难']; const difficulties = [1,2,3,4]; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('难度级别', style: TextStyle(fontSize: 16)), const SizedBox(height: 8), Wrap( spacing: 8, children: difficulties.map((level) { return ChoiceChip( label: Text(level.toString()), selected: challenge.difficulty == level, onSelected: (selected) { if (selected) { ref.read(challengeEditProvider.notifier).updateDifficulty(level); } }, ); }).toList(), ), ], ); } }