From b185e4434e503ff8f0f4eb07083ad4ad6cb9c1be Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Fri, 18 Apr 2025 21:33:22 +0200 Subject: [PATCH] Improved Button behaviour with input checker --- lib/views/round_view.dart | 65 +++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/views/round_view.dart b/lib/views/round_view.dart index 8e473b1..ebf6699 100644 --- a/lib/views/round_view.dart +++ b/lib/views/round_view.dart @@ -22,12 +22,8 @@ class _RoundViewState extends State { /// Index of the player who said CABO. int _caboPlayerIndex = 0; - int? _kamikazePlayerIndex; // null = kein Kamikaze, sonst Spielerindex - - /// List of booleans that represent whether the kamikaze checkbox is checked - /// for a player on that index. - late final List _isKamikazeChecked = - List.filled(widget.gameSession.players.length, false); + /// Index of the player who has Kamikaze. Default is null (no kamikaze player). + int? _kamikazePlayerIndex; /// List of text controllers for the point text fields. late final List _pointControllers = List.generate( @@ -233,26 +229,26 @@ class _RoundViewState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ CupertinoButton( + onPressed: _areRoundInputsValid() + ? () => {_finishRound(), Navigator.pop(context)} + : null, child: const Text('Fertig'), - onPressed: () => Navigator.pop(context), ), CupertinoButton( - child: const Text('Nächste Runde'), - onPressed: () => { - print('===================================='), - print('Runde ${widget.roundNumber} beendet'), - if (widget.roundNumber >= - widget.gameSession.playerScores[0].length) - {gameSession.expandPlayerScoreLists()}, - _checkForWinner(), - widget.gameSession.sumPoints(), - Navigator.pushReplacement(context, - CupertinoPageRoute(builder: (context) { - return RoundView( - gameSession: widget.gameSession, - roundNumber: widget.roundNumber + 1); - })), - }), + onPressed: _areRoundInputsValid() + ? () => { + _finishRound(), + Navigator.pushReplacement(context, + CupertinoPageRoute( + builder: (context) { + return RoundView( + gameSession: widget.gameSession, + roundNumber: + widget.roundNumber + 1); + })), + } + : null, + child: const Text('Nächste Runde')), ], )), ), @@ -261,6 +257,29 @@ class _RoundViewState extends State { )); } + bool _areRoundInputsValid() { + if (_kamikazePlayerIndex == null) { + return false; + } else { + for (TextEditingController c in _pointControllers) { + if (c.text.isEmpty) { + return false; + } + } + } + return true; + } + + void _finishRound() { + print('===================================='); + print('Runde ${widget.roundNumber} beendet'); + if (widget.roundNumber >= widget.gameSession.playerScores[0].length) { + gameSession.expandPlayerScoreLists(); + } + _checkForWinner(); + widget.gameSession.sumPoints(); + } + /// Checks the scores of the current round and assigns points to the players. /// There are three possible outcomes of a round: ///