1
1
// Level 1 | Difficulty Easy |
2
2
3
3
import 'dart:async' ;
4
+ import 'package:flutter/foundation.dart' ;
4
5
import 'package:flutter/material.dart' ;
5
6
import '../games/select_the_area/select_the_area_screen.dart' ;
6
7
import '../main/components/header.dart' ;
8
+ import 'wrapper/game_wrapper.dart' ;
7
9
8
10
class GameScreen extends StatefulWidget {
9
11
@override
@@ -17,30 +19,40 @@ class _GameScreenState extends State<GameScreen> {
17
19
late Timer _timer;
18
20
bool _gameLoaded = false ; // Tracks if the game is loaded
19
21
bool _gameOver = false ; // Tracks if the game is over
22
+ bool _isStarted = false ; // Tracks if the timer is started
20
23
21
24
@override
22
25
void initState () {
23
26
super .initState ();
24
27
}
25
28
26
29
void startTimer () {
27
- const Duration oneSec = Duration (seconds: 1 );
28
- _timer = Timer .periodic (
29
- oneSec,
30
- (Timer timer) {
31
- if (_progress <= 0 || _timeLeft <= 0 ) {
32
- timer.cancel ();
33
- setState (() {
34
- _gameOver = true ;
35
- });
36
- } else {
37
- setState (() {
38
- _progress -= 1 / 20 ; // Decrease progress based on total time
39
- _timeLeft -= 1 ;
40
- });
41
- }
42
- },
43
- );
30
+ if (! _isStarted) {
31
+ _isStarted = true ;
32
+ _timer = Timer .periodic (
33
+ const Duration (seconds: 1 ),
34
+ (Timer timer) {
35
+ if (_progress <= 0 || _timeLeft <= 0 ) {
36
+ timer.cancel ();
37
+ setState (() {
38
+ _gameOver = true ;
39
+ });
40
+ } else {
41
+ setState (() {
42
+ _timeLeft -= 1 ;
43
+
44
+ _progress =
45
+ _timeLeft / 20 ; // Decrease progress based on total time
46
+
47
+ if (kDebugMode) {
48
+ print ('Time Left: $_timeLeft ' );
49
+ print ('Progress: $_progress ' );
50
+ }
51
+ });
52
+ }
53
+ },
54
+ );
55
+ }
44
56
}
45
57
46
58
void updateScore (int points) {
@@ -157,21 +169,3 @@ class _GameScreenState extends State<GameScreen> {
157
169
);
158
170
}
159
171
}
160
-
161
- // Assume that the SelectTheAreaGame widget has a callback for when the game is loaded
162
- class GameWrapper extends StatelessWidget {
163
- const GameWrapper (
164
- {super .key, required this .onLoaded, required this .onScoreUpdate});
165
- final VoidCallback onLoaded;
166
- final ValueChanged <int > onScoreUpdate;
167
-
168
- @override
169
- Widget build (BuildContext context) {
170
- // Simulate a delay for loading the game
171
- Future .delayed (const Duration (seconds: 4 ), onLoaded);
172
-
173
- return const Center (
174
- child: SelectTheAreaGame (),
175
- );
176
- }
177
- }
0 commit comments