2024-05-10 22:00:14 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:werwolf/screens/settings.dart';
|
|
|
|
|
2024-06-18 01:46:57 +02:00
|
|
|
// Define the PlayerRegistry StatefulWidget
|
2024-05-10 22:00:14 +02:00
|
|
|
class PlayerRegistry extends StatefulWidget {
|
|
|
|
const PlayerRegistry({super.key});
|
|
|
|
|
|
|
|
@override
|
2024-06-18 01:46:57 +02:00
|
|
|
// Create the state for the PlayerRegistry widget
|
2024-06-07 20:08:07 +02:00
|
|
|
// ignore: library_private_types_in_public_api
|
2024-05-10 22:00:14 +02:00
|
|
|
_PlayerRegistryState createState() => _PlayerRegistryState();
|
|
|
|
}
|
|
|
|
|
2024-06-18 01:46:57 +02:00
|
|
|
// Define the state for the PlayerRegistry widget
|
2024-05-10 22:00:14 +02:00
|
|
|
class _PlayerRegistryState extends State<PlayerRegistry> {
|
2024-06-18 01:46:57 +02:00
|
|
|
final TextEditingController _playerController = TextEditingController(); // Controller for player name input
|
|
|
|
final _formKey = GlobalKey<FormState>(); // Key for form validation
|
|
|
|
String _errorMessage = ""; // Error message string
|
|
|
|
List<String> playernames = []; // List to store player names
|
2024-05-10 22:00:14 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-06-18 01:46:57 +02:00
|
|
|
title: const Text("Werwolf"), // App bar title
|
2024-05-10 22:00:14 +02:00
|
|
|
centerTitle: true,
|
|
|
|
),
|
2024-06-07 20:02:06 +02:00
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(15),
|
|
|
|
child: TextField(
|
|
|
|
key: _formKey,
|
|
|
|
controller: _playerController,
|
|
|
|
decoration: InputDecoration(
|
2024-06-18 01:46:57 +02:00
|
|
|
errorText: _errorMessage == "" ? null : _errorMessage, // Display error message if exists
|
|
|
|
labelText: 'Spielername', // Label for text field
|
2024-06-07 20:02:06 +02:00
|
|
|
focusedBorder: OutlineInputBorder(
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.primary),
|
|
|
|
),
|
|
|
|
enabledBorder: OutlineInputBorder(
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.primary),
|
|
|
|
),
|
|
|
|
errorBorder: OutlineInputBorder(
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.secondary),
|
|
|
|
),
|
|
|
|
focusedErrorBorder: OutlineInputBorder(
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.secondary),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onSubmitted: (value) {
|
|
|
|
setState(() {
|
|
|
|
if (_playerController.text.isEmpty) {
|
2024-06-18 01:46:57 +02:00
|
|
|
_errorMessage = "Spielername ist leer!"; // Error for empty name
|
2024-06-07 20:02:06 +02:00
|
|
|
} else if (playernames.contains(value)) {
|
2024-06-18 01:46:57 +02:00
|
|
|
_errorMessage = "Dieser Spieler existiert bereits"; // Error for duplicate name
|
2024-06-07 20:02:06 +02:00
|
|
|
_playerController.clear();
|
|
|
|
} else {
|
|
|
|
_errorMessage = "";
|
|
|
|
_playerController.clear();
|
2024-06-18 01:46:57 +02:00
|
|
|
playernames.add(value); // Add valid player name to list
|
2024-06-07 20:02:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-05-10 22:00:14 +02:00
|
|
|
),
|
|
|
|
),
|
2024-06-07 20:02:06 +02:00
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: playernames.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ListTile(
|
|
|
|
title: Text(
|
2024-06-18 01:46:57 +02:00
|
|
|
playernames[index], // Display player name
|
2024-06-07 20:02:06 +02:00
|
|
|
),
|
|
|
|
trailing: IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
2024-06-18 01:46:57 +02:00
|
|
|
playernames.remove(playernames[index]); // Remove player name from list
|
2024-06-07 20:02:06 +02:00
|
|
|
});
|
|
|
|
},
|
2024-06-18 01:46:57 +02:00
|
|
|
icon: const Icon(Icons.remove)), // Remove icon button
|
2024-06-07 20:02:06 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (playernames.length >= 6) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => GameSettings(
|
2024-06-18 01:46:57 +02:00
|
|
|
playernames: playernames, // Pass player names to next screen
|
2024-06-07 20:02:06 +02:00
|
|
|
),
|
|
|
|
));
|
2024-05-10 22:00:14 +02:00
|
|
|
} else {
|
2024-06-07 20:02:06 +02:00
|
|
|
setState(() {
|
2024-06-18 01:46:57 +02:00
|
|
|
_errorMessage = "Es müssen mindestens 6 Spieler sein!"; // Error for not enough players
|
2024-06-07 20:02:06 +02:00
|
|
|
});
|
2024-05-10 22:00:14 +02:00
|
|
|
}
|
|
|
|
},
|
2024-06-18 01:46:57 +02:00
|
|
|
child: const Text('Spiel einstellen'), // Button to start game settings
|
2024-05-10 22:00:14 +02:00
|
|
|
),
|
2024-06-07 20:08:07 +02:00
|
|
|
const Padding(padding: EdgeInsets.all(30))
|
2024-06-07 20:02:06 +02:00
|
|
|
],
|
|
|
|
),
|
2024-05-10 22:00:14 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|