Square root game design changed

This commit is contained in:
mehulmk
2022-02-22 16:19:56 +05:30
parent a448f808dc
commit 72a6e104c7
6 changed files with 123 additions and 233 deletions

View File

@ -25,7 +25,7 @@ class CalculatorProvider extends GameProvider<Calculator> {
loadNewDataIfRequired(); loadNewDataIfRequired();
_result = ""; _result = "";
if (timerStatus != TimerStatus.pause) { if (timerStatus != TimerStatus.pause) {
startTimer(); restartTimer();
} }
notifyListeners(); notifyListeners();
} else if (_result.length == currentState.answer.toString().length) { } else if (_result.length == currentState.answer.toString().length) {

View File

@ -40,7 +40,7 @@ class DialogListener<T extends GameProvider> extends StatelessWidget {
barrierDismissible: false, barrierDismissible: false,
).then((value) { ).then((value) {
if (value != null && value) { if (value != null && value) {
context.read<T>().restart(); context.read<T>().restartTimer();
} else { } else {
Navigator.pop(context); Navigator.pop(context);
} }
@ -63,7 +63,7 @@ class DialogListener<T extends GameProvider> extends StatelessWidget {
isScrollControlled: true, isScrollControlled: true,
).then((value) { ).then((value) {
if (value != null && value) { if (value != null && value) {
context.read<T>().restart(); context.read<T>().restartTimer();
} else { } else {
Navigator.pop(context); Navigator.pop(context);
} }

View File

@ -90,7 +90,7 @@ class TimeProvider with ChangeNotifier {
_animationController.value = 1.0; _animationController.value = 1.0;
} }
void restart() { void restartTimer() {
_animationController.reverse(from: 1.0); _animationController.reverse(from: 1.0);
timerStatus = TimerStatus.play; timerStatus = TimerStatus.play;
dialogType = DialogType.non; dialogType = DialogType.non;

View File

@ -1,139 +1,124 @@
import 'package:flutter/material.dart'; 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/ui/squareRoot/square_root_view_model.dart';
import 'package:mathgame/src/core/app_constant.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:provider/provider.dart';
import 'package:vsync_provider/vsync_provider.dart';
class SquareRootView extends StatelessWidget { class SquareRootView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider<SquareRootProvider>( return MultiProvider(
create: (_) => SquareRootProvider(), providers: [
child: WillPopScope( const VsyncProvider(),
onWillPop: () => Future.value(false), ChangeNotifierProvider<SquareRootProvider>(
child: Scaffold( create: (context) =>
body: SafeArea( SquareRootProvider(vsync: VsyncProvider.of(context)))
],
child: SafeArea(
top: true, top: true,
bottom: true, bottom: true,
child: Scaffold(
appBar: CommonAppBar<SquareRootProvider>(),
body: DialogListener<SquareRootProvider>(
gameCategoryType: GameCategoryType.SQUARE_ROOT,
child: Container( child: Container(
margin: EdgeInsets.fromLTRB(20, 20, 20, 0), margin: EdgeInsets.all(24),
constraints: BoxConstraints.expand(), constraints: BoxConstraints.expand(),
child: Consumer<SquareRootProvider>( child: Column(
builder: (context, squareRootProvider, child) {
return Column(
children: <Widget>[ children: <Widget>[
CommonInfoTextView<SquareRootProvider>(
gameCategoryType: GameCategoryType.SQUARE_ROOT),
Expanded( 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(
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( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: [
Expanded( Selector<SquareRootProvider, SquareRoot>(
child: Row( selector: (p0, p1) => p1.currentState,
children: <Widget>[ builder: (context, currentState, child) {
SquareRootButton( return Text(
squareRootProvider currentState.question,
.currentState.firstAns, style: Theme.of(context)
BorderRadius.only( .textTheme
topLeft: .subtitle2!
Radius.circular(40))), .copyWith(fontSize: 30),
SquareRootButton(
squareRootProvider
.currentState.secondAns,
BorderRadius.only(
topRight:
Radius.circular(40)))
],
),
),
Expanded(
child: Row(
children: <Widget>[
SquareRootButton(
squareRootProvider
.currentState.thirdAns,
BorderRadius.only(
bottomLeft:
Radius.circular(40))),
SquareRootButton(
squareRootProvider
.currentState.fourthAns,
BorderRadius.only(
bottomRight:
Radius.circular(40)))
],
),
)
],
),
),
),
),
),
Expanded(
flex: 10,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Consumer<SquareRootProvider>(
builder: (context, provider, child) {
return IconButton(
icon: provider.pause
? Icon(Icons.play_arrow)
: Icon(Icons.pause),
iconSize: 40,
onPressed: () {
provider.pauseTimer();
},
); );
}), }),
Consumer<SquareRootProvider>( SizedBox(height: 14),
builder: (context, provider, child) { Neumorphic(
return IconButton( style: NeumorphicStyle(
icon: Icon(Icons.info_outline), // shape: NeumorphicShape.convex,
iconSize: 40, boxShape: NeumorphicBoxShape.roundRect(
onPressed: () { BorderRadius.circular(18)),
provider.showInfoDialog(); depth: -8,
}, lightSource: LightSource.topLeft,
color: Theme.of(context).colorScheme.iconBgColor,
),
child: Container(
height: 30,
width: 30,
alignment: Alignment.center,
margin: const EdgeInsets.only(
left: 10, right: 10, bottom: 5, top: 10),
child: Selector<SquareRootProvider, String>(
selector: (p0, p1) => p1.result,
builder: (context, calculatorProvider, child) {
return Text(
calculatorProvider,
style: Theme.of(context)
.textTheme
.subtitle2!
.copyWith(
fontSize: 30, color: Color(0xff4895EF)),
); );
}) },
),
),
),
], ],
)), ),
),
Selector<SquareRootProvider, SquareRoot>(
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<SquareRootProvider>()
.checkResult(e);
},
startColor: Color(0xff4895EF),
endColor: Color(0xff3f37c9),
);
},
)
], ],
); );
}, },
)), ),
],
),
),
), ),
), ),
), ),

View File

@ -3,130 +3,35 @@ import 'dart:async';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:mathgame/src/data/models/square_root.dart'; import 'package:mathgame/src/data/models/square_root.dart';
import 'package:mathgame/src/core/app_constant.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<SquareRoot> {
late GameViewModelImp gameViewModel;
class SquareRootProvider extends GameProvider<SquareRoot> {
late String _result; late String _result;
String get result => _result; String get result => _result;
late int _time; SquareRootProvider({required TickerProvider vsync})
: super(vsync: vsync, gameCategoryType: GameCategoryType.SQUARE_ROOT) {
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<SquareRoot>(
gameAccess: this, gameCategoryType: GameCategoryType.SQUARE_ROOT);
startGame1();
}
@override
void startGame1() {
_time = TimeUtil.squareRootTimeOut;
_timeOut = false;
_result = ""; _result = "";
_currentScore = 0; startGame();
gameViewModel.startGame();
} }
Future<void> checkResult(String answer) async { Future<void> checkResult(String answer) async {
if (_result.length < currentState.answer.toString().length && !_timeOut) { if (_result.length < currentState.answer.toString().length &&
timerStatus != TimerStatus.pause) {
_result = answer; _result = answer;
notifyListeners(); notifyListeners();
if (int.parse(_result) == currentState.answer) { if (int.parse(_result) == currentState.answer) {
await Future.delayed(Duration(milliseconds: 300)); await Future.delayed(Duration(milliseconds: 300));
/*<<<<<<< HEAD loadNewDataIfRequired();
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();
_result = ""; _result = "";
_time = TimeUtil.squareRootTimeOut; if (timerStatus != TimerStatus.pause) {
if (!_timeOut) { restartTimer();
gameViewModel.restartGame();
} }
notifyListeners(); notifyListeners();
} else if (_result.length == currentState.answer.toString().length) { } else if (_result.length == currentState.answer.toString().length) {
gameViewModel.wrongAnswer(); wrongAnswer();
_result = "";
} }
} }
} }
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();
}
} }

View File

@ -25,7 +25,7 @@ class SignProvider extends GameProvider<Sign> {
loadNewDataIfRequired(); loadNewDataIfRequired();
_result = ""; _result = "";
if (timerStatus != TimerStatus.pause) { if (timerStatus != TimerStatus.pause) {
startTimer(); restartTimer();
} }
notifyListeners(); notifyListeners();
} else { } else {