253 lines
9.5 KiB
Dart
253 lines
9.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../models/activity.dart';
|
|
import '../services/activity_service.dart';
|
|
import '../utils/constants.dart';
|
|
import 'activity_detail_screen.dart';
|
|
import 'create_activity_screen.dart';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
String _selectedCategory = '全部';
|
|
String _timeFilter = '全部';
|
|
List<Activity> _activities = [];
|
|
bool _loading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadActivities();
|
|
}
|
|
|
|
void _loadActivities() {
|
|
setState(() {
|
|
_activities = ActivityService.getMockActivities();
|
|
_loading = false;
|
|
});
|
|
}
|
|
|
|
List<Activity> get _filteredActivities {
|
|
return _activities.where((a) {
|
|
if (_selectedCategory != '全部' && a.category != _selectedCategory) return false;
|
|
if (_timeFilter == '今天') {
|
|
return DateUtils.isSameDay(a.time, DateTime.now());
|
|
}
|
|
if (_timeFilter == '明天') {
|
|
final tomorrow = DateTime.now().add(const Duration(days: 1));
|
|
return DateUtils.isSameDay(a.time, tomorrow);
|
|
}
|
|
return true;
|
|
}).toList();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.location_on, size: 18, color: Color(0xFF1976D2)),
|
|
const SizedBox(width: 4),
|
|
const Text('成都', style: TextStyle(fontSize: 16, color: Color(0xFF666666))),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: Container(
|
|
height: 36,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFF5F5F5),
|
|
borderRadius: BorderRadius.circular(18),
|
|
),
|
|
child: const Row(
|
|
children: [
|
|
SizedBox(width: 12),
|
|
Icon(Icons.search, size: 20, color: Color(0xFF999999)),
|
|
SizedBox(width: 8),
|
|
Text('搜索活动', style: TextStyle(fontSize: 14, color: Color(0xFF999999))),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
body: _loading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: RefreshIndicator(
|
|
onRefresh: () async => _loadActivities(),
|
|
child: CustomScrollView(
|
|
slivers: [
|
|
// Category chips
|
|
SliverToBoxAdapter(
|
|
child: SizedBox(
|
|
height: 48,
|
|
child: ListView(
|
|
scrollDirection: Axis.horizontal,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
children: AppConstants.activityCategories.map((cat) {
|
|
final selected = _selectedCategory == cat;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: ChoiceChip(
|
|
label: Text(cat, style: TextStyle(fontSize: 14, color: selected ? Colors.white : const Color(0xFF666666))),
|
|
selected: selected,
|
|
selectedColor: const Color(0xFF1976D2),
|
|
backgroundColor: const Color(0xFFF5F5F5),
|
|
onSelected: (_) => setState(() => _selectedCategory = cat),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
),
|
|
// Time filter
|
|
SliverToBoxAdapter(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Row(
|
|
children: ['全部', '今天', '明天', '本周'].map((t) {
|
|
final selected = _timeFilter == t;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(right: 12),
|
|
child: GestureDetector(
|
|
onTap: () => setState(() => _timeFilter = t),
|
|
child: Text(t, style: TextStyle(
|
|
fontSize: 16,
|
|
color: selected ? const Color(0xFF1976D2) : const Color(0xFF999999),
|
|
fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
|
|
)),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
),
|
|
// Activity list
|
|
SliverList(
|
|
delegate: SliverChildBuilderDelegate(
|
|
(ctx, i) => _ActivityCard(
|
|
key: ValueKey(_filteredActivities[i].id),
|
|
activity: _filteredActivities[i],
|
|
onTap: () => Navigator.push(context, MaterialPageRoute(
|
|
builder: (_) => ActivityDetailScreen(activity: _filteredActivities[i]),
|
|
)),
|
|
),
|
|
childCount: _filteredActivities.length,
|
|
),
|
|
),
|
|
const SliverToBoxAdapter(child: SizedBox(height: 80)),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => const CreateActivityScreen())),
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('发起活动', style: TextStyle(fontSize: 16)),
|
|
backgroundColor: const Color(0xFF1976D2),
|
|
foregroundColor: Colors.white,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ActivityCard extends StatefulWidget {
|
|
final Activity activity;
|
|
final VoidCallback onTap;
|
|
const _ActivityCard({super.key, required this.activity, required this.onTap});
|
|
|
|
@override
|
|
State<_ActivityCard> createState() => _ActivityCardState();
|
|
}
|
|
|
|
class _ActivityCardState extends State<_ActivityCard> {
|
|
bool _isJoined = false;
|
|
|
|
String _categoryEmoji(String cat) {
|
|
const map = {'太极':'🧘','茶话会':'🍵','书法':'✍️','摄影':'📷','舞蹈':'💃','户外徒步':'🥾','手工':'🎨','唱歌':'🎵','棋牌':'♟️','读书':'📚','晨练':'🏃'};
|
|
return map[cat] ?? '🎯';
|
|
}
|
|
|
|
void _joinActivity() {
|
|
setState(() {
|
|
_isJoined = true;
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('报名成功!')),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final activity = widget.activity;
|
|
final canJoin = !activity.isFull && !_isJoined;
|
|
final buttonText = _isJoined
|
|
? '已报名'
|
|
: (activity.isFull ? '已满员' : '报名');
|
|
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
|
child: InkWell(
|
|
onTap: widget.onTap,
|
|
borderRadius: BorderRadius.circular(16),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Text(_categoryEmoji(activity.category), style: const TextStyle(fontSize: 24)),
|
|
const SizedBox(width: 8),
|
|
Expanded(child: Text(activity.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600), maxLines: 1, overflow: TextOverflow.ellipsis)),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.access_time, size: 16, color: Color(0xFF999999)),
|
|
const SizedBox(width: 4),
|
|
Text(activity.timeDisplay, style: const TextStyle(fontSize: 16, color: Color(0xFF666666))),
|
|
const SizedBox(width: 16),
|
|
const Icon(Icons.location_on_outlined, size: 16, color: Color(0xFF999999)),
|
|
const SizedBox(width: 4),
|
|
Text(activity.distance != null ? '距你${activity.distance}km' : '', style: const TextStyle(fontSize: 16, color: Color(0xFF666666))),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
'已报名 ${activity.currentParticipants}/${activity.maxParticipants}人',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: activity.isFull ? const Color(0xFFF44336) : const Color(0xFF4CAF50),
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: 40,
|
|
child: ElevatedButton(
|
|
onPressed: canJoin ? _joinActivity : null,
|
|
style: ElevatedButton.styleFrom(
|
|
minimumSize: const Size(80, 40),
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
|
),
|
|
child: Text(buttonText, style: const TextStyle(fontSize: 16)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|