From 72a6e104c77d76debf602b93b863a7dd9c380f0d Mon Sep 17 00:00:00 2001 From: mehulmk Date: Tue, 22 Feb 2022 16:19:56 +0530 Subject: [PATCH] Square root game design changed --- .../ui/calculator/calculator_provider.dart | 2 +- lib/src/ui/common/dialog_listener.dart | 4 +- lib/src/ui/common/time_provider.dart | 2 +- lib/src/ui/squareRoot/square_root_view.dart | 227 ++++++++---------- .../ui/squareRoot/square_root_view_model.dart | 119 +-------- lib/src/ui/whatsTheSign/sign_view_model.dart | 2 +- 6 files changed, 123 insertions(+), 233 deletions(-) diff --git a/lib/src/ui/calculator/calculator_provider.dart b/lib/src/ui/calculator/calculator_provider.dart index 252746e..2e5761c 100755 --- a/lib/src/ui/calculator/calculator_provider.dart +++ b/lib/src/ui/calculator/calculator_provider.dart @@ -25,7 +25,7 @@ class CalculatorProvider extends GameProvider { loadNewDataIfRequired(); _result = ""; if (timerStatus != TimerStatus.pause) { - startTimer(); + restartTimer(); } notifyListeners(); } else if (_result.length == currentState.answer.toString().length) { diff --git a/lib/src/ui/common/dialog_listener.dart b/lib/src/ui/common/dialog_listener.dart index ecff07d..052f610 100644 --- a/lib/src/ui/common/dialog_listener.dart +++ b/lib/src/ui/common/dialog_listener.dart @@ -40,7 +40,7 @@ class DialogListener extends StatelessWidget { barrierDismissible: false, ).then((value) { if (value != null && value) { - context.read().restart(); + context.read().restartTimer(); } else { Navigator.pop(context); } @@ -63,7 +63,7 @@ class DialogListener extends StatelessWidget { isScrollControlled: true, ).then((value) { if (value != null && value) { - context.read().restart(); + context.read().restartTimer(); } else { Navigator.pop(context); } diff --git a/lib/src/ui/common/time_provider.dart b/lib/src/ui/common/time_provider.dart index 3179a65..03623a4 100644 --- a/lib/src/ui/common/time_provider.dart +++ b/lib/src/ui/common/time_provider.dart @@ -90,7 +90,7 @@ class TimeProvider with ChangeNotifier { _animationController.value = 1.0; } - void restart() { + void restartTimer() { _animationController.reverse(from: 1.0); timerStatus = TimerStatus.play; dialogType = DialogType.non; diff --git a/lib/src/ui/squareRoot/square_root_view.dart b/lib/src/ui/squareRoot/square_root_view.dart index 284fa4c..da52351 100755 --- a/lib/src/ui/squareRoot/square_root_view.dart +++ b/lib/src/ui/squareRoot/square_root_view.dart @@ -1,139 +1,124 @@ import 'package:flutter/material.dart'; +import 'package:flutter_neumorphic/flutter_neumorphic.dart'; +import 'package:mathgame/src/core/color_scheme.dart'; +import 'package:mathgame/src/data/models/square_root.dart'; +import 'package:mathgame/src/ui/common/CommonNumberButton.dart'; +import 'package:mathgame/src/ui/common/common_app_bar.dart'; +import 'package:mathgame/src/ui/common/common_info_text_view.dart'; +import 'package:mathgame/src/ui/common/dialog_listener.dart'; import 'package:mathgame/src/ui/squareRoot/square_root_view_model.dart'; import 'package:mathgame/src/core/app_constant.dart'; -import 'package:mathgame/src/ui/squareRoot/square_root_button.dart'; -import 'package:mathgame/src/ui/common/timer.dart'; import 'package:provider/provider.dart'; +import 'package:vsync_provider/vsync_provider.dart'; class SquareRootView extends StatelessWidget { @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (_) => SquareRootProvider(), - child: WillPopScope( - onWillPop: () => Future.value(false), + return MultiProvider( + providers: [ + const VsyncProvider(), + ChangeNotifierProvider( + create: (context) => + SquareRootProvider(vsync: VsyncProvider.of(context))) + ], + child: SafeArea( + top: true, + bottom: true, child: Scaffold( - body: SafeArea( - top: true, - bottom: true, + appBar: CommonAppBar(), + body: DialogListener( + gameCategoryType: GameCategoryType.SQUARE_ROOT, child: Container( - margin: EdgeInsets.fromLTRB(20, 20, 20, 0), - constraints: BoxConstraints.expand(), - child: Consumer( - builder: (context, squareRootProvider, child) { - return Column( - children: [ - Expanded( - flex: 10, - child: SizedBox( - child: Align( - alignment: Alignment.bottomLeft, - child: Timer(GameCategoryType.SQUARE_ROOT), - ), - )), - Expanded( - flex: 20, - child: Center( - child: Visibility( - visible: !squareRootProvider.pause, - child: Text( - squareRootProvider.currentState.question, - style: Theme.of(context).textTheme.headline1, - ), - ), - )), - Expanded( - flex: 60, - child: Align( + margin: EdgeInsets.all(24), + constraints: BoxConstraints.expand(), + child: Column( + children: [ + CommonInfoTextView( + gameCategoryType: GameCategoryType.SQUARE_ROOT), + Expanded( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Selector( + selector: (p0, p1) => p1.currentState, + builder: (context, currentState, child) { + return Text( + currentState.question, + style: Theme.of(context) + .textTheme + .subtitle2! + .copyWith(fontSize: 30), + ); + }), + SizedBox(height: 14), + Neumorphic( + style: NeumorphicStyle( + // shape: NeumorphicShape.convex, + boxShape: NeumorphicBoxShape.roundRect( + BorderRadius.circular(18)), + depth: -8, + lightSource: LightSource.topLeft, + color: Theme.of(context).colorScheme.iconBgColor, + ), + child: Container( + height: 30, + width: 30, alignment: Alignment.center, - child: SizedBox( - height: 300, - width: 500, - child: Container( - margin: EdgeInsets.fromLTRB(20, 0, 20, 0), - decoration: BoxDecoration( - borderRadius: - BorderRadius.all(Radius.circular(30)), - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Expanded( - child: Row( - children: [ - SquareRootButton( - squareRootProvider - .currentState.firstAns, - BorderRadius.only( - topLeft: - Radius.circular(40))), - SquareRootButton( - squareRootProvider - .currentState.secondAns, - BorderRadius.only( - topRight: - Radius.circular(40))) - ], - ), - ), - Expanded( - child: Row( - children: [ - SquareRootButton( - squareRootProvider - .currentState.thirdAns, - BorderRadius.only( - bottomLeft: - Radius.circular(40))), - SquareRootButton( - squareRootProvider - .currentState.fourthAns, - BorderRadius.only( - bottomRight: - Radius.circular(40))) - ], - ), - ) - ], - ), - ), + margin: const EdgeInsets.only( + left: 10, right: 10, bottom: 5, top: 10), + child: Selector( + selector: (p0, p1) => p1.result, + builder: (context, calculatorProvider, child) { + return Text( + calculatorProvider, + style: Theme.of(context) + .textTheme + .subtitle2! + .copyWith( + fontSize: 30, color: Color(0xff4895EF)), + ); + }, ), ), ), - Expanded( - flex: 10, - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Consumer( - builder: (context, provider, child) { - return IconButton( - icon: provider.pause - ? Icon(Icons.play_arrow) - : Icon(Icons.pause), - iconSize: 40, - onPressed: () { - provider.pauseTimer(); - }, - ); - }), - Consumer( - builder: (context, provider, child) { - return IconButton( - icon: Icon(Icons.info_outline), - iconSize: 40, - onPressed: () { - provider.showInfoDialog(); - }, - ); - }) - ], - )), ], - ); - }, - )), + ), + ), + Selector( + selector: (p0, p1) => p1.currentState, + builder: (context, currentState, child) { + return GridView( + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2), + shrinkWrap: true, + physics: NeverScrollableScrollPhysics(), + children: [ + ...[ + currentState.firstAns, + currentState.secondAns, + currentState.thirdAns, + currentState.fourthAns, + ].map( + (e) { + return CommonNumberButton( + text: e, + onTab: () { + context + .read() + .checkResult(e); + }, + startColor: Color(0xff4895EF), + endColor: Color(0xff3f37c9), + ); + }, + ) + ], + ); + }, + ), + ], + ), + ), ), ), ), diff --git a/lib/src/ui/squareRoot/square_root_view_model.dart b/lib/src/ui/squareRoot/square_root_view_model.dart index 4d5ba3b..be526ab 100755 --- a/lib/src/ui/squareRoot/square_root_view_model.dart +++ b/lib/src/ui/squareRoot/square_root_view_model.dart @@ -3,130 +3,35 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:mathgame/src/data/models/square_root.dart'; import 'package:mathgame/src/core/app_constant.dart'; -import 'package:mathgame/src/core/time_constant.dart'; +import 'package:mathgame/src/ui/common/game_provider.dart'; -import '../common/game_view_model.dart'; - -class SquareRootProvider - with ChangeNotifier - implements GameAccess { - late GameViewModelImp gameViewModel; - - late String _result; +class SquareRootProvider extends GameProvider { + late String _result; String get result => _result; - late int _time; - - int get time => _time; - - bool _pause = false; - - bool get pause => _pause; - - late SquareRoot _currentState; - - SquareRoot get currentState => _currentState; - - double _currentScore = 0; - - double get currentScore => _currentScore; - late bool _timeOut; - - SquareRootProvider() { - gameViewModel = GameViewModelImp( - gameAccess: this, gameCategoryType: GameCategoryType.SQUARE_ROOT); - startGame1(); - } - - @override - void startGame1() { - _time = TimeUtil.squareRootTimeOut; - _timeOut = false; + SquareRootProvider({required TickerProvider vsync}) + : super(vsync: vsync, gameCategoryType: GameCategoryType.SQUARE_ROOT) { _result = ""; - _currentScore = 0; - gameViewModel.startGame(); + startGame(); } Future checkResult(String answer) async { - if (_result.length < currentState.answer.toString().length && !_timeOut) { + if (_result.length < currentState.answer.toString().length && + timerStatus != TimerStatus.pause) { _result = answer; notifyListeners(); if (int.parse(_result) == currentState.answer) { await Future.delayed(Duration(milliseconds: 300)); -/*<<<<<<< HEAD - if (_list.length - 1 == _index) { - _list.addAll( - SquareRootQandSDataProvider.getSquareDataList(_index ~/ 5 + 2)); - print("_index $_index"); - } - _index = _index + 1; - currentScore = currentScore + ScoreUtil.squareRootScore; - _currentState = _list[_index]; -=======*/ - gameViewModel.loadNewDataIfRequired(); + loadNewDataIfRequired(); _result = ""; - _time = TimeUtil.squareRootTimeOut; - if (!_timeOut) { - gameViewModel.restartGame(); + if (timerStatus != TimerStatus.pause) { + restartTimer(); } notifyListeners(); } else if (_result.length == currentState.answer.toString().length) { - gameViewModel.wrongAnswer(); - _result = ""; + wrongAnswer(); } } } - - clear() { - _result = ""; - notifyListeners(); - } - - @override - void onGameTimeOut() { - this._timeOut = true; - } - - @override - void onGameTimeUpdate(int time) { - _time = time; - notifyListeners(); - } - - @override - void onCurrentStateUpdate(SquareRoot currentState) { - _currentState = currentState; - notifyListeners(); - } - - @override - void onScoreUpdate(double time) { - _currentScore = time; - notifyListeners(); - } - - @override - void onResumeGame() { - _pause = false; - notifyListeners(); - } - - void pauseTimer() { - _pause = true; - gameViewModel.pauseGame(); - notifyListeners(); - gameViewModel.showPauseGameDialog(); - } - - void showInfoDialog() { - _pause = true; - gameViewModel.pauseGame(); - gameViewModel.showInfoDialog(); - } - - void dispose() { - super.dispose(); - gameViewModel.exitGame(); - } } diff --git a/lib/src/ui/whatsTheSign/sign_view_model.dart b/lib/src/ui/whatsTheSign/sign_view_model.dart index 7fa2bf4..b938b2b 100755 --- a/lib/src/ui/whatsTheSign/sign_view_model.dart +++ b/lib/src/ui/whatsTheSign/sign_view_model.dart @@ -25,7 +25,7 @@ class SignProvider extends GameProvider { loadNewDataIfRequired(); _result = ""; if (timerStatus != TimerStatus.pause) { - startTimer(); + restartTimer(); } notifyListeners(); } else {