Aktuelle Version mit richtigem Shuffle
This commit is contained in:
@@ -1,31 +1,23 @@
|
||||
// lib/features/tournament/presentation/screens/fields_screen.dart
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../data/models/player_model.dart';
|
||||
import '../provider/active_players_provider.dart';
|
||||
import '../provider/player_provider.dart'; // für awardWinToPlayers
|
||||
import '../widgets/volleyball_field_widget.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import '../../data/models/player_model.dart';
|
||||
import '../provider/player_provider.dart';
|
||||
import '../provider/shuffle_provider.dart'; // <-- jetzt mit shuffleResultProvider + shuffleControllerProvider
|
||||
import '../widgets/volleyball_field_widget.dart';
|
||||
|
||||
class FieldsScreen extends ConsumerWidget {
|
||||
const FieldsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final activePlayers = ref.watch(activePlayersProvider);
|
||||
|
||||
// Shuffle für Demo (später echter Algorithmus)
|
||||
final shuffled = List<PlayerModel>.from(activePlayers)..shuffle();
|
||||
|
||||
// Maximal 36 Spieler auf Felder, Rest Aussetzer
|
||||
final playersOnFields = shuffled.length > 36 ? shuffled.sublist(0, 36) : shuffled;
|
||||
final bench = shuffled.length > 36 ? shuffled.sublist(36) : <PlayerModel>[];
|
||||
|
||||
// Felder aufteilen
|
||||
final field1 = _slice(playersOnFields, 0, 12);
|
||||
final field2 = _slice(playersOnFields, 12, 24);
|
||||
final field3 = _slice(playersOnFields, 24, 36);
|
||||
|
||||
final fields = [field1, field2, field3];
|
||||
// Hier lesen wir den aktuellen Shuffle-Stand aus
|
||||
final shuffleResult = ref.watch(shuffleResultProvider);
|
||||
final fields = shuffleResult.fields;
|
||||
final bench = shuffleResult.bench;
|
||||
final message = shuffleResult.message;
|
||||
|
||||
return Scaffold(
|
||||
body: Column(
|
||||
@@ -35,9 +27,11 @@ class FieldsScreen extends ConsumerWidget {
|
||||
child: PageView.builder(
|
||||
itemCount: 3,
|
||||
itemBuilder: (context, index) {
|
||||
final fieldPlayers = fields[index];
|
||||
final fieldPlayers = index < fields.length ? fields[index] : <PlayerModel>[];
|
||||
|
||||
final teamA = fieldPlayers.sublist(0, fieldPlayers.length.clamp(0, 6));
|
||||
final teamA = fieldPlayers.isNotEmpty
|
||||
? fieldPlayers.sublist(0, fieldPlayers.length.clamp(0, 6))
|
||||
: <PlayerModel>[];
|
||||
final teamB = fieldPlayers.length > 6
|
||||
? fieldPlayers.sublist(6, fieldPlayers.length.clamp(6, 12))
|
||||
: <PlayerModel>[];
|
||||
@@ -46,11 +40,14 @@ class FieldsScreen extends ConsumerWidget {
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(child: VolleyballFieldWidget(teamA: teamA, teamB: teamB, fieldNumber: index + 1)),
|
||||
|
||||
Expanded(
|
||||
child: VolleyballFieldWidget(
|
||||
teamA: teamA,
|
||||
teamB: teamB,
|
||||
fieldNumber: index + 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Gewinner-Buttons
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
@@ -74,7 +71,7 @@ class FieldsScreen extends ConsumerWidget {
|
||||
SnackBar(
|
||||
content: Text('Oben gewinnt! +1 Schleifchen für ${teamA.length} Spieler'),
|
||||
backgroundColor: Colors.green,
|
||||
duration: const Duration(seconds: 1),
|
||||
duration: const Duration(milliseconds: 800),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -95,14 +92,14 @@ class FieldsScreen extends ConsumerWidget {
|
||||
),
|
||||
onPressed: teamB.isEmpty
|
||||
? null
|
||||
: () async{
|
||||
HapticFeedback.mediumImpact();
|
||||
: () async {
|
||||
await HapticFeedback.mediumImpact();
|
||||
ref.read(playerListProvider.notifier).awardWinToPlayers(teamB);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Unten gewinnt! +1 Schleifchen für ${teamB.length} Spieler'),
|
||||
backgroundColor: Colors.red,
|
||||
duration: const Duration(seconds: 1),
|
||||
duration: const Duration(milliseconds: 800),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -131,28 +128,30 @@ class FieldsScreen extends ConsumerWidget {
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
children: bench
|
||||
.map((p) => Chip(
|
||||
label: Text(p.name),
|
||||
backgroundColor: Colors.orange.shade300,
|
||||
))
|
||||
.map((p) => Chip(label: Text(p.name), backgroundColor: Colors.orange.shade300))
|
||||
.toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Shuffle Button unten
|
||||
// NEUER SHUFFLE-BUTTON – ruft den Controller auf
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: ElevatedButton.icon(
|
||||
icon: const Icon(Icons.shuffle),
|
||||
label: const Text('Neu mischen', style: TextStyle(fontSize: 18)),
|
||||
onPressed: () async{
|
||||
HapticFeedback.mediumImpact();
|
||||
ref.invalidate(activePlayersProvider);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Neue Teams gemischt!'), duration: Duration(milliseconds: 100)),
|
||||
);
|
||||
label: Text(
|
||||
message.isEmpty ? 'Neu mischen' : message,
|
||||
style: const TextStyle(fontSize: 18),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
backgroundColor: Colors.deepPurple,
|
||||
),
|
||||
onPressed: () async {
|
||||
await HapticFeedback.selectionClick();
|
||||
// Das ist der entscheidende Aufruf!
|
||||
ref.read(shuffleControllerProvider).shuffle();
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -160,11 +159,4 @@ class FieldsScreen extends ConsumerWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Hilfsfunktion für sicheres Slicing
|
||||
List<PlayerModel> _slice(List<PlayerModel> list, int start, int end) {
|
||||
final actualEnd = end.clamp(0, list.length);
|
||||
if (start >= actualEnd) return [];
|
||||
return list.sublist(start, actualEnd);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user