1+ import 'package:add_numbers/schema/blackSchema.dart' ;
12import 'package:add_numbers/widgets/bgGradient.dart' ;
23import 'package:add_numbers/provider/BlockDataStream.dart' ;
34import 'package:add_numbers/widgets/targetBlockBuilder.dart' ;
@@ -9,26 +10,93 @@ class GameScreen extends StatefulWidget {
910}
1011
1112class _GameScreenState extends State <GameScreen > {
12- NumberCreater numberCreater = NumberCreater ();
13+ BlockDataStream blockDataStream = BlockDataStream ();
1314
14- List blocks = getBlocks ();
15+ BlockSchema blockSchema;
16+ List <BlockSchema > blocks;
17+
18+ int currentTotal;
1519
1620 @override
1721 void initState () {
1822 super .initState ();
19- numberCreater.stream.listen ((data) {
23+ this .fillBlocksData ();
24+ this .listenBlockChanges ();
25+ }
26+
27+ void fillBlocksData () {
28+ setState (() {
29+ this .currentTotal = 0 ;
30+ this .blockSchema = BlockSchema ();
31+ this .blocks = blockSchema.getBlocks ();
32+ });
33+ }
34+
35+ void listenBlockChanges () {
36+ blockDataStream.stream.listen ((Map <String , int > blockData) {
2037 setState (() {
21- if (this .blocks[data].color != Colors .green) {
22- this .blocks[data].color = Colors .green;
23- }
38+ _validateBlocks (blockData);
2439 });
2540 });
2641 }
2742
43+ void _validateBlocks (blockData) {
44+ int selectedIndex = blockData['index' ];
45+
46+ if (this .blocks[selectedIndex].isSelected) {
47+ return ;
48+ }
49+
50+ this .blocks[selectedIndex].isSelected = true ;
51+
52+ this .currentTotal += blockData['value' ];
53+
54+ if (this .currentTotal < this .blockSchema.target) {
55+ changeBlockColor (selectedIndex, Colors .green);
56+ if (! isThereChanceToMakeItCorrect ()) {
57+ _wrongAnswer (selectedIndex);
58+ return ;
59+ }
60+ } else if (this .currentTotal == this .blockSchema.target) {
61+ _correctAnswer (selectedIndex);
62+ } else {
63+ _wrongAnswer (selectedIndex);
64+ }
65+ }
66+
67+ void _wrongAnswer (selectedIndex) {
68+ changeBlockColor (selectedIndex, Colors .red);
69+ _showStatusAlert ('Wrong !!!' , Icons .clear, Colors .red, false );
70+ }
71+
72+ void _correctAnswer (selectedIndex) {
73+ changeBlockColor (selectedIndex, Colors .green);
74+ _showStatusAlert ('Correct !!!' , Icons .check, Colors .green, true );
75+ }
76+
77+ bool isThereChanceToMakeItCorrect () {
78+ bool chance = false ;
79+
80+ for (var i = 0 ; i < this .blocks.length; i++ ) {
81+ if (this .blocks[i].isSelected) {
82+ continue ;
83+ }
84+
85+ if (this .currentTotal + this .blocks[i].value <= this .blockSchema.target) {
86+ chance = true ;
87+ }
88+ }
89+
90+ return chance;
91+ }
92+
93+ void changeBlockColor (selectedIndex, Color color) {
94+ this .blocks[selectedIndex].color = color;
95+ }
96+
2897 @override
2998 Widget build (BuildContext context) {
30- return MaterialApp (
31- home: SafeArea (
99+ return SafeArea (
32100 child: Scaffold (
33101 body: Container (
34102 decoration: bgBoxDecoration (),
@@ -37,7 +105,8 @@ class _GameScreenState extends State<GameScreen> {
37105 SizedBox (
38106 height: 40 ,
39107 ),
40- buildTargetBlock (title: 'Target' , targetValue: 6 ),
108+ buildTargetBlock (
109+ title: 'Target' , targetValue: this .blockSchema.target),
41110 SizedBox (
42111 height: 6 ,
43112 ),
@@ -46,7 +115,7 @@ class _GameScreenState extends State<GameScreen> {
46115 ),
47116 ),
48117 ),
49- )) ;
118+ );
50119 }
51120
52121 Container buildNumberBlocks () {
@@ -55,34 +124,69 @@ class _GameScreenState extends State<GameScreen> {
55124 alignment: Alignment (0.0 , 0.0 ),
56125 // color: Colors.grey,
57126 padding: const EdgeInsets .all (30 ),
58- child: Wrap (
59- spacing: 40 ,
60- runSpacing: 40 ,
61- children: < Widget > [
62- _numberBlock (
63- bgColor: this .blocks[0 ].color, value: this .blocks[0 ].value),
64- _numberBlock (
65- bgColor: this .blocks[1 ].color, value: this .blocks[1 ].value),
66- _numberBlock (
67- bgColor: this .blocks[2 ].color, value: this .blocks[2 ].value),
68- _numberBlock (
69- bgColor: this .blocks[3 ].color, value: this .blocks[3 ].value),
70- _numberBlock (
71- bgColor: this .blocks[4 ].color, value: this .blocks[4 ].value),
72- _numberBlock (
73- bgColor: this .blocks[5 ].color, value: this .blocks[5 ].value),
74- ],
75- ),
127+ child: Wrap (spacing: 40 , runSpacing: 40 , children: _generateBlocks (6 )),
128+ );
129+ }
130+
131+ List <Widget > _generateBlocks (int size) {
132+ List <Widget > blocks = [];
133+
134+ for (var i = 0 ; i < size; i++ ) {
135+ blocks.add (_numberBlock (
136+ bgColor: this .blocks[i].color,
137+ index: this .blocks[i].index,
138+ value: this .blocks[i].value));
139+ }
140+
141+ return blocks;
142+ }
143+
144+ Future <void > _showStatusAlert (
145+ String title, IconData icon, Color color, bool isSuccess) async {
146+ return showDialog <void >(
147+ context: context,
148+ barrierDismissible: false , // user must tap button!
149+ builder: (BuildContext context) {
150+ return AlertDialog (
151+ title: Text (
152+ title,
153+ style: TextStyle (fontSize: 20 ),
154+ textAlign: TextAlign .center,
155+ ),
156+ content: SingleChildScrollView (
157+ child: Center (
158+ child: Icon (
159+ icon,
160+ color: color,
161+ size: 100 ,
162+ ),
163+ ),
164+ ),
165+ actions: < Widget > [
166+ FlatButton (
167+ child: Text (
168+ 'Solve Another one' ,
169+ textAlign: TextAlign .center,
170+ ),
171+ onPressed: () {
172+ fillBlocksData ();
173+ Navigator .of (context).pop ();
174+ },
175+ ),
176+ ],
177+ );
178+ },
76179 );
77180 }
78181
79- Widget _numberBlock ({Color borderColor, Color bgColor, int value}) {
182+ Widget _numberBlock (
183+ {Color borderColor, Color bgColor, int index, int value}) {
80184 bool isSelected = false ;
81185 return Material (
82186 child: InkWell (
83187 onTap: () {
84188 isSelected = isSelected ? false : true ;
85- numberCreater .setCount (value);
189+ blockDataStream .setCount (index : index, value : value);
86190 }, // handle your onTap here
87191 child: Container (
88192 width: 120 ,
@@ -102,21 +206,3 @@ class _GameScreenState extends State<GameScreen> {
102206 );
103207 }
104208}
105-
106- class Block {
107- Color color;
108- int value;
109- int index;
110- Block (this .color, this .value);
111- }
112-
113- List <Block > getBlocks () {
114- return [
115- new Block (Colors .yellow, 0 ),
116- new Block (Colors .red, 1 ),
117- new Block (Colors .blue, 2 ),
118- new Block (Colors .yellow, 3 ),
119- new Block (Colors .red, 4 ),
120- new Block (Colors .blue, 5 )
121- ];
122- }
0 commit comments