210 lines
9.0 KiB
Dart
210 lines
9.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'training_detail_screen.dart';
|
|
|
|
class FavoritesTab extends StatefulWidget {
|
|
final String? categoryFilter;
|
|
const FavoritesTab({super.key, this.categoryFilter});
|
|
|
|
@override
|
|
State<FavoritesTab> createState() => _FavoritesTabState();
|
|
}
|
|
|
|
class _FavoritesTabState extends State<FavoritesTab> {
|
|
static const List<String> _categories = [
|
|
'Aufwärmen & Mobilisation',
|
|
'Wurf- & Torabschluss',
|
|
'Torwarttraining',
|
|
'Athletik',
|
|
'Pass',
|
|
'Koordination',
|
|
];
|
|
String? _selectedCategory;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selectedCategory = widget.categoryFilter;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final user = FirebaseAuth.instance.currentUser;
|
|
if (user == null) {
|
|
return const Center(child: Text('Nicht eingeloggt'));
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Favoriten'),
|
|
),
|
|
body: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Filter-Chip-Leiste
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
|
child: Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
FilterChip(
|
|
label: const Text('Alle'),
|
|
selected: _selectedCategory == null,
|
|
onSelected: (selected) {
|
|
setState(() => _selectedCategory = null);
|
|
},
|
|
),
|
|
..._categories.map((cat) => FilterChip(
|
|
label: Text(cat),
|
|
selected: _selectedCategory == cat,
|
|
onSelected: (selected) {
|
|
setState(() => _selectedCategory = selected ? cat : null);
|
|
},
|
|
)),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: StreamBuilder<DocumentSnapshot>(
|
|
stream: FirebaseFirestore.instance.collection('User').doc(user.uid).snapshots(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (!snapshot.hasData || !snapshot.data!.exists) {
|
|
return const Center(child: Text('Keine Favoriten gefunden'));
|
|
}
|
|
final data = snapshot.data!.data() as Map<String, dynamic>;
|
|
final allFavorites = List<String>.from(data['favorites'] ?? []);
|
|
|
|
if (allFavorites.isEmpty) {
|
|
return const Center(child: Text('Keine Favoriten gefunden'));
|
|
}
|
|
|
|
// Lade alle Favoriten-Dokumente auf einmal
|
|
return FutureBuilder<List<DocumentSnapshot>>(
|
|
future: Future.wait(allFavorites.map((id) =>
|
|
FirebaseFirestore.instance.collection('Training').doc(id).get()
|
|
)),
|
|
builder: (context, multiSnapshot) {
|
|
if (multiSnapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (!multiSnapshot.hasData) {
|
|
return const Center(child: Text('Favoriten konnten nicht geladen werden'));
|
|
}
|
|
|
|
// Filtere die Favoriten basierend auf der Kategorie
|
|
final filteredFavorites = multiSnapshot.data!.where((doc) {
|
|
if (!doc.exists) return false;
|
|
if (_selectedCategory == null) return true;
|
|
final trainingData = doc.data() as Map<String, dynamic>;
|
|
return trainingData['category'] == _selectedCategory;
|
|
}).toList();
|
|
|
|
if (filteredFavorites.isEmpty) {
|
|
return const Center(child: Text('Keine Favoriten in dieser Kategorie gefunden'));
|
|
}
|
|
|
|
return GridView.builder(
|
|
padding: const EdgeInsets.all(8),
|
|
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
|
maxCrossAxisExtent: 300,
|
|
childAspectRatio: 0.75,
|
|
crossAxisSpacing: 10,
|
|
mainAxisSpacing: 10,
|
|
),
|
|
itemCount: filteredFavorites.length,
|
|
itemBuilder: (context, index) {
|
|
final doc = filteredFavorites[index];
|
|
final trainingData = doc.data() as Map<String, dynamic>;
|
|
|
|
return Card(
|
|
child: Stack(
|
|
children: [
|
|
InkWell(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => TrainingDetailScreen(trainingId: doc.id),
|
|
),
|
|
);
|
|
},
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Expanded(
|
|
child: Container(
|
|
color: Colors.grey[200],
|
|
child: const Center(
|
|
child: Icon(Icons.fitness_center, size: 50),
|
|
),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
trainingData['title'] ?? 'Unbekannt',
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
const SizedBox(height: 4),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.star, color: Colors.amber, size: 16),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
(trainingData['rating overall'] ?? 0.0).toStringAsFixed(1),
|
|
style: const TextStyle(fontSize: 12),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'Dauer: \t${trainingData['duration'] ?? '-'} Minuten',
|
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Positioned(
|
|
top: 4,
|
|
right: 4,
|
|
child: IconButton(
|
|
icon: const Icon(Icons.favorite, color: Colors.red),
|
|
onPressed: () async {
|
|
await FirebaseFirestore.instance.collection('User').doc(user.uid).update({
|
|
'favorites': FieldValue.arrayRemove([doc.id]),
|
|
});
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|