From ea391fc0e83124453b5179a68b38a44a63ed9f54 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Sat, 18 Mar 2023 14:00:35 +0100 Subject: [PATCH] make buttons selectable following scoe of the previous throw :zap: --- .../bowlin_project/lib/model/GamePlayer.dart | 27 ++++---- .../lib/views/ingame_screen2.dart | 38 ++++++------ .../lib/widgets/ingame_widgets.dart | 61 ++++++++++++++----- 3 files changed, 79 insertions(+), 47 deletions(-) diff --git a/Sources/bowlin_project/lib/model/GamePlayer.dart b/Sources/bowlin_project/lib/model/GamePlayer.dart index 826440e..03b5489 100644 --- a/Sources/bowlin_project/lib/model/GamePlayer.dart +++ b/Sources/bowlin_project/lib/model/GamePlayer.dart @@ -5,8 +5,7 @@ import 'package:go_router/go_router.dart'; import 'Round.dart'; -class GamePlayer{ - +class GamePlayer { late GameDetail _game; int currentRoundIndex = 0; @@ -17,29 +16,31 @@ class GamePlayer{ _game = value; } - void onNext(bool isRoundFinished, BuildContext context){ - if(isRoundFinished){ + void onNext(bool isRoundFinished, BuildContext context) { + if (isRoundFinished) { print("++"); currentRoundIndex++; } - if(currentRoundIndex>=game.rounds.length){ + if (currentRoundIndex >= game.rounds.length) { print("FINISHED"); context.go("/scoreboard"); - }else{ + } else { print("IN GAME : " + currentRoundIndex.toString()); - if(game.rounds[currentRoundIndex] is Round){ + if (game.rounds[currentRoundIndex] is Round) { print("ROUND"); - }else{ + } else { print("LAST ROUND"); } - context.pushReplacement("/in-game", extra: game.rounds[currentRoundIndex]); + context.pushReplacement("/in-game", + extra: game.rounds[currentRoundIndex]); } } - void onSpareOrStrike(){ - if(currentRoundIndex { late InGameCardThrow widgetHolder; int selectedValue = 0; - void setSelectedValue(int val){ - selectedValue=val; + void setSelectedValue(int val) { + selectedValue = val; } - void initState() { - - if (widget.currentRound.firstThrow == null) - widgetHolder = InGameCardThrow(numberThrow: 1, currentRound: - widget.currentRound, setSelectedValue: setSelectedValue); - else if (widget.currentRound.secondThrow == null) { - widgetHolder = InGameCardThrow(numberThrow: 2 - , currentRound: - widget.currentRound, setSelectedValue: setSelectedValue); + if (widget.currentRound.firstThrow == null) { + widgetHolder = InGameCardThrow( + numberThrow: 1, + currentRound: widget.currentRound, + setSelectedValue: setSelectedValue); + } else if (widget.currentRound.secondThrow == null) { + widgetHolder = InGameCardThrow( + numberThrow: 2, + currentRound: widget.currentRound, + setSelectedValue: setSelectedValue); } else { - widgetHolder = InGameCardThrow(numberThrow: 3, currentRound: - widget.currentRound, setSelectedValue: setSelectedValue); + widgetHolder = InGameCardThrow( + numberThrow: 3, + currentRound: widget.currentRound, + setSelectedValue: setSelectedValue); } super.initState(); @@ -44,7 +47,6 @@ class _InGameScreen2State extends State { @override Widget build(BuildContext context) { - return Stack( children: [ Container( @@ -70,11 +72,10 @@ class _InGameScreen2State extends State { Spacer(), ElevatedButton( onPressed: () { - bool isFinished = widget.currentRound.computeNext( - selectedValue - ); + bool isFinished = + widget.currentRound.computeNext(selectedValue); - if(widget.currentRound.isSpareOrStrike()){ + if (widget.currentRound.isSpareOrStrike()) { MyApp.controller.gamePlayer.onSpareOrStrike(); } @@ -99,7 +100,6 @@ class _InGameScreen2State extends State { ), minimumSize: Size(200, 80), ), - ), Spacer(), ], diff --git a/Sources/bowlin_project/lib/widgets/ingame_widgets.dart b/Sources/bowlin_project/lib/widgets/ingame_widgets.dart index 4101af1..17b8b25 100644 --- a/Sources/bowlin_project/lib/widgets/ingame_widgets.dart +++ b/Sources/bowlin_project/lib/widgets/ingame_widgets.dart @@ -350,7 +350,12 @@ class InGameCardThrow extends StatefulWidget { final int numberThrow; final AbstractRound currentRound; final Function(int) setSelectedValue; - const InGameCardThrow({Key? key, required this.numberThrow, required this.currentRound, required this.setSelectedValue}): super(key: key); + const InGameCardThrow( + {Key? key, + required this.numberThrow, + required this.currentRound, + required this.setSelectedValue}) + : super(key: key); @override State createState() => _InGameCardThrowState(); @@ -358,6 +363,19 @@ class InGameCardThrow extends StatefulWidget { class _InGameCardThrowState extends State { GlobalKey<_NumberPadState> _numberPadKey = GlobalKey(); + late var maxValue; + + void initState() { + if (widget.currentRound.firstThrow == null) { + maxValue = 10; + } else if (widget.currentRound.secondThrow == null) { + maxValue = 10 - (widget.currentRound.firstThrow ?? 0); + } else { + maxValue = 10; + } + + super.initState(); + } @override Widget build(BuildContext context) { @@ -393,9 +411,14 @@ class _InGameCardThrowState extends State { fontWeight: FontWeight.w400, decoration: TextDecoration.none)), TextSpan( - text: widget.currentRound.number.toString() + " - " + - widget.numberThrow.toString()+ - (widget.numberThrow==1 ? "st" : widget.numberThrow==2? "nd" : "rd"),//'1 - 1st', + text: widget.currentRound.number.toString() + + " - " + + widget.numberThrow.toString() + + (widget.numberThrow == 1 + ? "st" + : widget.numberThrow == 2 + ? "nd" + : "rd"), //'1 - 1st', style: GoogleFonts.roboto( fontSize: 18, color: CupertinoColors.black, @@ -424,7 +447,8 @@ class _InGameCardThrowState extends State { NumberPad( numberThrow: widget.numberThrow, setSelectedValue: widget.setSelectedValue, - currentRound: widget.currentRound + currentRound: widget.currentRound, + maxValue: maxValue, ), ], ), @@ -539,7 +563,14 @@ class NumberPad extends StatefulWidget { final int numberThrow; final AbstractRound currentRound; final Function(int) setSelectedValue; - const NumberPad({Key? key, required this.numberThrow, required this.setSelectedValue, required this.currentRound}) : super(key: key); + final int maxValue; + const NumberPad( + {Key? key, + required this.numberThrow, + required this.setSelectedValue, + required this.currentRound, + required this.maxValue}) + : super(key: key); @override State createState() => _NumberPadState(); @@ -576,7 +607,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 1, - isSelectable: 1, + isSelectable: widget.maxValue > 1 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -584,7 +615,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 2, - isSelectable: 1, + isSelectable: widget.maxValue > 2 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -592,7 +623,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 3, - isSelectable: 1, + isSelectable: widget.maxValue > 3 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -605,7 +636,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 4, - isSelectable: 1, + isSelectable: widget.maxValue > 4 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -613,7 +644,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 5, - isSelectable: 1, + isSelectable: widget.maxValue > 5 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -621,7 +652,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 6, - isSelectable: 1, + isSelectable: widget.maxValue > 6 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -634,7 +665,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 7, - isSelectable: 1, + isSelectable: widget.maxValue > 7 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -642,7 +673,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 8, - isSelectable: 0, + isSelectable: widget.maxValue > 8 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); }, @@ -650,7 +681,7 @@ class _NumberPadState extends State { Number( currentSelected: NumSelected, num: 9, - isSelectable: 0, + isSelectable: widget.maxValue > 9 ? 1 : 0, onSonChanged: (int newId) { updateId(newId); },