Skip to content

Commit e3e76c6

Browse files
committedJul 2, 2024·
timer fixed
1 parent 0e4c42b commit e3e76c6

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed
 

‎lib/screens/games/game_screen.dart

+29-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Level 1 | Difficulty Easy |
22

33
import 'dart:async';
4+
import 'package:flutter/foundation.dart';
45
import 'package:flutter/material.dart';
56
import '../games/select_the_area/select_the_area_screen.dart';
67
import '../main/components/header.dart';
8+
import 'wrapper/game_wrapper.dart';
79

810
class GameScreen extends StatefulWidget {
911
@override
@@ -17,30 +19,40 @@ class _GameScreenState extends State<GameScreen> {
1719
late Timer _timer;
1820
bool _gameLoaded = false; // Tracks if the game is loaded
1921
bool _gameOver = false; // Tracks if the game is over
22+
bool _isStarted = false; // Tracks if the timer is started
2023

2124
@override
2225
void initState() {
2326
super.initState();
2427
}
2528

2629
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+
}
4456
}
4557

4658
void updateScore(int points) {
@@ -157,21 +169,3 @@ class _GameScreenState extends State<GameScreen> {
157169
);
158170
}
159171
}
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-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Assume that the SelectTheAreaGame widget has a callback for when the game is loaded
2+
import 'package:flutter/material.dart';
3+
4+
import '../select_the_area/select_the_area_screen.dart';
5+
6+
class GameWrapper extends StatelessWidget {
7+
const GameWrapper(
8+
{super.key, required this.onLoaded, required this.onScoreUpdate});
9+
final VoidCallback onLoaded;
10+
final ValueChanged<int> onScoreUpdate;
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
// Simulate a delay for loading the game
15+
Future.delayed(const Duration(seconds: 4), onLoaded);
16+
17+
return const Center(
18+
child: SelectTheAreaGame(),
19+
);
20+
}
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.