| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // challenge_list_screen.dart
- 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_list_vm.dart';
- import 'package:japp_flutter/features/challenge/views/widgets/challenge_card.dart';
- class ChallengeListScreen extends ConsumerWidget {
- const ChallengeListScreen({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final challengesState = ref.watch(challengeListProvider);
- return Scaffold(
- appBar: AppBar(
- title: const Text('挑战列表'),
- actions: [
- IconButton(
- icon: const Icon(Icons.refresh),
- onPressed: () => ref.read(challengeListProvider.notifier).refresh(),
- ),
- ],
- ),
- body: _buildBody(challengesState, ref),
- floatingActionButton: FloatingActionButton(
- onPressed: () => Navigator.pushNamed(context, '/challenge/add'),
- child: const Icon(Icons.add),
- ),
- );
- }
- Widget _buildBody(AsyncValue<List<ChallengeModel>> state, WidgetRef ref) {
- return state.when(
- loading: () => const Center(child: CircularProgressIndicator()),
- error: (error, stack) => Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Icon(Icons.error, color: Colors.red, size: 48),
- const SizedBox(height: 16),
- Text('加载失败: $error', style: const TextStyle(color: Colors.red)),
- const SizedBox(height: 16),
- ElevatedButton(
- onPressed: () =>
- ref.read(challengeListProvider.notifier).refresh(),
- child: const Text('重试'),
- ),
- ],
- ),
- ),
- data: (challenges) => challenges.isEmpty
- ? const Center(child: Text('暂无挑战', style: TextStyle(fontSize: 18)))
- : RefreshIndicator(
- onRefresh: () =>
- ref.read(challengeListProvider.notifier).refresh(),
- child: ListView.builder(
- padding: const EdgeInsets.all(16),
- itemCount: challenges.length,
- itemBuilder: (_, index) => ChallengeCard(challenge: challenges[index]),
-
- // itemBuilder: (context, index) => ListTile(
- // title: Text(challenges[index].title),
- // // 其他列表项内容
- // ),
- ),
- ),
- );
- }
- }
|