Training selection und erstellen von Trainern
parent
1a96eb25fe
commit
59b13d21e2
|
|
@ -1,4 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
|
|
||||||
class SearchTab extends StatefulWidget {
|
class SearchTab extends StatefulWidget {
|
||||||
const SearchTab({super.key});
|
const SearchTab({super.key});
|
||||||
|
|
@ -17,6 +19,38 @@ class _SearchTabState extends State<SearchTab> {
|
||||||
'Mobility',
|
'Mobility',
|
||||||
'Rehabilitation',
|
'Rehabilitation',
|
||||||
];
|
];
|
||||||
|
String? _selectedCategory;
|
||||||
|
String _searchTerm = '';
|
||||||
|
bool _isTrainer = false;
|
||||||
|
bool _trainerChecked = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_searchController.addListener(() {
|
||||||
|
setState(() {
|
||||||
|
_searchTerm = _searchController.text.trim();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
_checkIfTrainer();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _checkIfTrainer() async {
|
||||||
|
final user = FirebaseAuth.instance.currentUser;
|
||||||
|
if (user == null) return;
|
||||||
|
final doc = await FirebaseFirestore.instance.collection('User').doc(user.uid).get();
|
||||||
|
setState(() {
|
||||||
|
_isTrainer = doc.data()?['role'] == 'trainer';
|
||||||
|
_trainerChecked = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _showCreateTrainingDialog() {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => _CreateTrainingDialog(categories: _categories),
|
||||||
|
).then((_) => setState(() {})); // Refresh nach Hinzufügen
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
|
@ -37,6 +71,14 @@ class _SearchTabState extends State<SearchTab> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
actions: [
|
||||||
|
if (_trainerChecked && _isTrainer)
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
tooltip: 'Neues Training erstellen',
|
||||||
|
onPressed: _showCreateTrainingDialog,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
SliverPadding(
|
SliverPadding(
|
||||||
padding: const EdgeInsets.all(16.0),
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
|
@ -52,72 +94,130 @@ class _SearchTabState extends State<SearchTab> {
|
||||||
Wrap(
|
Wrap(
|
||||||
spacing: 8,
|
spacing: 8,
|
||||||
runSpacing: 8,
|
runSpacing: 8,
|
||||||
children:
|
children: _categories.map((category) {
|
||||||
_categories.map((category) {
|
return FilterChip(
|
||||||
return FilterChip(
|
label: Text(category),
|
||||||
label: Text(category),
|
selected: _selectedCategory == category,
|
||||||
onSelected: (bool selected) {
|
onSelected: (bool selected) {
|
||||||
// TODO: Implement category filtering
|
setState(() {
|
||||||
},
|
_selectedCategory = selected ? category : null;
|
||||||
);
|
});
|
||||||
}).toList(),
|
},
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SliverPadding(
|
if (_searchTerm.isNotEmpty || _selectedCategory != null)
|
||||||
padding: const EdgeInsets.all(16.0),
|
SliverPadding(
|
||||||
sliver: SliverGrid(
|
padding: const EdgeInsets.all(16.0),
|
||||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
sliver: FutureBuilder<QuerySnapshot>(
|
||||||
crossAxisCount: 2,
|
future: FirebaseFirestore.instance.collection('Training').get(),
|
||||||
mainAxisSpacing: 16,
|
builder: (context, snapshot) {
|
||||||
crossAxisSpacing: 16,
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
childAspectRatio: 0.75,
|
return const SliverToBoxAdapter(
|
||||||
),
|
child: Center(child: CircularProgressIndicator()));
|
||||||
delegate: SliverChildBuilderDelegate((context, index) {
|
}
|
||||||
return Card(
|
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
|
||||||
clipBehavior: Clip.antiAlias,
|
return const SliverToBoxAdapter(
|
||||||
child: Column(
|
child: Center(child: Text('Keine Trainings gefunden.')));
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
}
|
||||||
children: [
|
final docs = snapshot.data!.docs.where((doc) {
|
||||||
Expanded(
|
final data = doc.data() as Map<String, dynamic>;
|
||||||
child: Container(
|
final title = (data['title'] ?? '').toString().toLowerCase();
|
||||||
color: Colors.grey[300],
|
final description = (data['description'] ?? '').toString().toLowerCase();
|
||||||
child: const Center(
|
final category = (data['category'] ?? '').toString();
|
||||||
child: Icon(Icons.fitness_center, size: 40),
|
final matchesSearch = _searchTerm.isEmpty ||
|
||||||
),
|
title.contains(_searchTerm.toLowerCase()) ||
|
||||||
),
|
description.contains(_searchTerm.toLowerCase());
|
||||||
),
|
final matchesCategory = _selectedCategory == null || category == _selectedCategory;
|
||||||
Padding(
|
return matchesSearch && matchesCategory;
|
||||||
padding: const EdgeInsets.all(8.0),
|
}).toList();
|
||||||
|
if (docs.isEmpty) {
|
||||||
|
return const SliverToBoxAdapter(
|
||||||
|
child: Center(child: Text('Keine Trainings gefunden.')));
|
||||||
|
}
|
||||||
|
return SliverGrid(
|
||||||
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
|
crossAxisCount: 2,
|
||||||
|
mainAxisSpacing: 16,
|
||||||
|
crossAxisSpacing: 16,
|
||||||
|
childAspectRatio: 0.75,
|
||||||
|
),
|
||||||
|
delegate: SliverChildBuilderDelegate((context, index) {
|
||||||
|
final data = docs[index].data() as Map<String, dynamic>;
|
||||||
|
return Card(
|
||||||
|
clipBehavior: Clip.antiAlias,
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Expanded(
|
||||||
'Training ${index + 1}',
|
child: (data['picture'] is String && data['picture'] != '')
|
||||||
style: const TextStyle(
|
? Image.network(
|
||||||
fontWeight: FontWeight.bold,
|
data['picture'],
|
||||||
fontSize: 16,
|
width: double.infinity,
|
||||||
),
|
fit: BoxFit.cover,
|
||||||
|
)
|
||||||
|
: Container(
|
||||||
|
color: Colors.grey[300],
|
||||||
|
child: const Center(
|
||||||
|
child: Icon(Icons.fitness_center, size: 40),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
Padding(
|
||||||
Text(
|
padding: const EdgeInsets.all(8.0),
|
||||||
'${30 + index * 5} Minuten',
|
child: Column(
|
||||||
style: TextStyle(
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
color: Colors.grey[600],
|
children: [
|
||||||
fontSize: 14,
|
Text(
|
||||||
|
data['title'] ?? '-',
|
||||||
|
style: const TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 16,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
data['description'] ?? '-',
|
||||||
|
maxLines: 2,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
style: TextStyle(
|
||||||
|
color: Colors.grey[600],
|
||||||
|
fontSize: 13,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
'${data['duration'] ?? '-'} Minuten',
|
||||||
|
style: TextStyle(
|
||||||
|
color: Colors.grey[600],
|
||||||
|
fontSize: 13,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.star, size: 16, color: Colors.amber),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
Text('${data['rating overall'] ?? '-'}'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text('Level: ${data['year'] ?? '-'}'),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
);
|
||||||
],
|
}, childCount: docs.length),
|
||||||
),
|
);
|
||||||
);
|
},
|
||||||
}, childCount: 6),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
@ -129,3 +229,111 @@ class _SearchTabState extends State<SearchTab> {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _CreateTrainingDialog extends StatefulWidget {
|
||||||
|
final List<String> categories;
|
||||||
|
const _CreateTrainingDialog({required this.categories});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_CreateTrainingDialog> createState() => _CreateTrainingDialogState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CreateTrainingDialogState extends State<_CreateTrainingDialog> {
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
String? _category;
|
||||||
|
String? _title;
|
||||||
|
String? _description;
|
||||||
|
int? _duration;
|
||||||
|
String? _picture;
|
||||||
|
double? _rating;
|
||||||
|
String? _year;
|
||||||
|
bool _loading = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Neues Training erstellen'),
|
||||||
|
content: SingleChildScrollView(
|
||||||
|
child: Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
DropdownButtonFormField<String>(
|
||||||
|
value: _category,
|
||||||
|
items: widget.categories
|
||||||
|
.map((cat) => DropdownMenuItem(value: cat, child: Text(cat)))
|
||||||
|
.toList(),
|
||||||
|
onChanged: (v) => setState(() => _category = v),
|
||||||
|
decoration: const InputDecoration(labelText: 'Kategorie'),
|
||||||
|
validator: (v) => v == null ? 'Kategorie wählen' : null,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Titel'),
|
||||||
|
onChanged: (v) => _title = v,
|
||||||
|
validator: (v) => v == null || v.isEmpty ? 'Titel angeben' : null,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Beschreibung'),
|
||||||
|
onChanged: (v) => _description = v,
|
||||||
|
validator: (v) => v == null || v.isEmpty ? 'Beschreibung angeben' : null,
|
||||||
|
maxLines: 2,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Dauer (Minuten)'),
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
onChanged: (v) => _duration = int.tryParse(v),
|
||||||
|
validator: (v) => v == null || int.tryParse(v) == null ? 'Zahl angeben' : null,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Bild-URL (optional)'),
|
||||||
|
onChanged: (v) => _picture = v,
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Bewertung (0-5)'),
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
onChanged: (v) => _rating = double.tryParse(v),
|
||||||
|
validator: (v) {
|
||||||
|
final d = double.tryParse(v ?? '');
|
||||||
|
if (d == null || d < 0 || d > 5) return '0-5 angeben';
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TextFormField(
|
||||||
|
decoration: const InputDecoration(labelText: 'Schwierigkeitslevel'),
|
||||||
|
onChanged: (v) => _year = v,
|
||||||
|
validator: (v) => v == null || v.isEmpty ? 'Level angeben' : null,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: _loading ? null : () => Navigator.pop(context),
|
||||||
|
child: const Text('Abbrechen'),
|
||||||
|
),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: _loading
|
||||||
|
? null
|
||||||
|
: () async {
|
||||||
|
if (_formKey.currentState!.validate()) {
|
||||||
|
setState(() => _loading = true);
|
||||||
|
await FirebaseFirestore.instance.collection('Training').add({
|
||||||
|
'category': _category,
|
||||||
|
'title': _title,
|
||||||
|
'description': _description,
|
||||||
|
'duration': _duration,
|
||||||
|
'picture': _picture,
|
||||||
|
'rating overall': _rating,
|
||||||
|
'year': _year,
|
||||||
|
});
|
||||||
|
Navigator.pop(context);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: _loading ? const CircularProgressIndicator() : const Text('Erstellen'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue