import 'package:flutter/material.dart'; import 'package:werwolf/screens/settings.dart'; class PlayerRegistry extends StatefulWidget { const PlayerRegistry({super.key}); @override _PlayerRegistryState createState() => _PlayerRegistryState(); } class _PlayerRegistryState extends State { final TextEditingController _playerController = TextEditingController(); final _formKey = GlobalKey(); String _errorMessage = ""; List playernames = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Werwolf"), centerTitle: true, ), 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( errorText: _errorMessage == "" ? null : _errorMessage, labelText: 'Spielername', 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) { _errorMessage = "Spielername ist leer!"; } else if (playernames.contains(value)) { _errorMessage = "Dieser Spieler existiert bereits"; _playerController.clear(); } else { _errorMessage = ""; _playerController.clear(); playernames.add(value); } }); }, ), ), Expanded( child: ListView.builder( itemCount: playernames.length, itemBuilder: (context, index) { return ListTile( title: Text( playernames[index], ), trailing: IconButton( onPressed: () { setState(() { playernames.remove(playernames[index]); }); }, icon: const Icon(Icons.remove)), ); }, ), ), ElevatedButton( onPressed: () { if (playernames.length >= 6) { Navigator.push( context, MaterialPageRoute( builder: (context) => GameSettings( playernames: playernames, ), )); } else { setState(() { _errorMessage = "Es müssen mindestens 6 Spieler sein!"; }); } }, child: const Text('Spiel einstellen'), ), Padding(padding: EdgeInsets.all(30)) ], ), ), ); } }