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();
_result = "";
if (timerStatus != TimerStatus.pause) {
startTimer();
restartTimer();
}
notifyListeners();
} else if (_result.length == currentState.answer.toString().length) {

View File

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

View File

@ -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;

View File

@ -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<SquareRootProvider>(
create: (_) => SquareRootProvider(),
child: WillPopScope(
onWillPop: () => Future.value(false),
return MultiProvider(
providers: [
const VsyncProvider(),
ChangeNotifierProvider<SquareRootProvider>(
create: (context) =>
SquareRootProvider(vsync: VsyncProvider.of(context)))
],
child: SafeArea(
top: true,
bottom: true,
child: Scaffold(
body: SafeArea(
top: true,
bottom: true,
appBar: CommonAppBar<SquareRootProvider>(),
body: DialogListener<SquareRootProvider>(
gameCategoryType: GameCategoryType.SQUARE_ROOT,
child: Container(
margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
constraints: BoxConstraints.expand(),
child: Consumer<SquareRootProvider>(
builder: (context, squareRootProvider, child) {
return Column(
children: <Widget>[
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: <Widget>[
CommonInfoTextView<SquareRootProvider>(
gameCategoryType: GameCategoryType.SQUARE_ROOT),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Selector<SquareRootProvider, SquareRoot>(
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: <Widget>[
Expanded(
child: Row(
children: <Widget>[
SquareRootButton(
squareRootProvider
.currentState.firstAns,
BorderRadius.only(
topLeft:
Radius.circular(40))),
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)))
],
),
)
],
),
),
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)),
);
},
),
),
),
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>(
builder: (context, provider, child) {
return IconButton(
icon: Icon(Icons.info_outline),
iconSize: 40,
onPressed: () {
provider.showInfoDialog();
},
);
})
],
)),
],
);
},
)),
),
),
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: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<SquareRoot> {
late GameViewModelImp gameViewModel;
late String _result;
class SquareRootProvider extends GameProvider<SquareRoot> {
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<SquareRoot>(
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<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;
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();
}
}

View File

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