mohsen zamani
2 years ago
11 changed files with 235 additions and 83 deletions
-
8lib/application/notifiers/image_splitter_notifier.dart
-
6lib/cubits/base_cubit_type.dart
-
18lib/cubits/count_down_timer_cubit.dart
-
51lib/main.dart
-
71lib/screens/photo/photo_screen_large.dart
-
12lib/screens/puzzle/puzzle_starter_screen.dart
-
102lib/widgets/solo_screen/count_down_timer_widget.dart
-
1lib/widgets/solo_screen/countdown_widget.dart
-
16lib/widgets/solo_screen/game_button_widget.dart
-
32pubspec.lock
-
1pubspec.yaml
@ -0,0 +1,6 @@ |
|||
class BaseCubitType<T> { |
|||
BaseCubitType({this.eventName, this.data}); |
|||
|
|||
T? eventName; |
|||
dynamic data; |
|||
} |
@ -0,0 +1,18 @@ |
|||
import 'package:flutter_bloc/flutter_bloc.dart'; |
|||
import 'package:my_flutter_puzzle/cubits/base_cubit_type.dart'; |
|||
|
|||
class CountDownTimerCubit extends Cubit<BaseCubitType<CountDownTimerState>> { |
|||
CountDownTimerCubit() : super(BaseCubitType(eventName: CountDownTimerState.empty)); |
|||
|
|||
void empty() => emit(BaseCubitType(eventName: CountDownTimerState.empty)); |
|||
|
|||
void start() => emit(BaseCubitType(eventName: CountDownTimerState.start)); |
|||
|
|||
void stop() => emit(BaseCubitType(eventName: CountDownTimerState.stop)); |
|||
} |
|||
|
|||
enum CountDownTimerState { |
|||
empty, |
|||
start, |
|||
stop, |
|||
} |
@ -0,0 +1,102 @@ |
|||
import 'dart:async'; |
|||
|
|||
import 'package:flutter/material.dart'; |
|||
import 'package:flutter_bloc/flutter_bloc.dart'; |
|||
import 'package:my_flutter_puzzle/cubits/base_cubit_type.dart'; |
|||
import 'package:my_flutter_puzzle/cubits/count_down_timer_cubit.dart'; |
|||
|
|||
class CountDownTimerWidget extends StatefulWidget { |
|||
final int duration; |
|||
final Function() finishCallback; |
|||
|
|||
const CountDownTimerWidget({Key? key, required this.duration, required this.finishCallback}) : super(key: key); |
|||
|
|||
@override |
|||
State<CountDownTimerWidget> createState() => _CountDownTimerWidgetState(); |
|||
} |
|||
|
|||
class _CountDownTimerWidgetState extends State<CountDownTimerWidget> { |
|||
Timer? _countdownTimer; |
|||
Duration? myDuration; |
|||
late final CountDownTimerCubit _cubit; |
|||
|
|||
@override |
|||
void initState() { |
|||
_cubit = BlocProvider.of<CountDownTimerCubit>(context); |
|||
myDuration = Duration(minutes: widget.duration); |
|||
super.initState(); |
|||
} |
|||
|
|||
void startTimer() { |
|||
myDuration = Duration(minutes: widget.duration); |
|||
_countdownTimer = Timer.periodic(const Duration(seconds: 1), (_) => setCountDown()); |
|||
} |
|||
|
|||
void stopTimer() { |
|||
setState(() => _countdownTimer!.cancel()); |
|||
} |
|||
|
|||
void resetTimer() { |
|||
stopTimer(); |
|||
setState(() => myDuration = const Duration(days: 5)); |
|||
} |
|||
|
|||
void setCountDown() { |
|||
const reduceSecondsBy = 1; |
|||
setState(() { |
|||
final seconds = myDuration!.inSeconds - reduceSecondsBy; |
|||
if (seconds < 0) { |
|||
widget.finishCallback.call(); |
|||
_countdownTimer!.cancel(); |
|||
} else { |
|||
myDuration = Duration(seconds: seconds); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
@override |
|||
Widget build(BuildContext context) { |
|||
String strDigits(int n) => n.toString().padLeft(2, '0'); |
|||
final hours = strDigits(myDuration!.inHours.remainder(24)); |
|||
final minutes = strDigits(myDuration!.inMinutes.remainder(60)); |
|||
final seconds = strDigits(myDuration!.inSeconds.remainder(60)); |
|||
return BlocBuilder<CountDownTimerCubit, BaseCubitType<CountDownTimerState>>( |
|||
builder: (context, state) { |
|||
switch (state.eventName!) { |
|||
case CountDownTimerState.empty: |
|||
break; |
|||
case CountDownTimerState.start: |
|||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { |
|||
startTimer(); |
|||
_cubit.empty(); |
|||
}); |
|||
break; |
|||
case CountDownTimerState.stop: |
|||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { |
|||
stopTimer(); |
|||
_cubit.empty(); |
|||
}); |
|||
break; |
|||
} |
|||
return Row( |
|||
children: [ |
|||
Text( |
|||
'$hours:$minutes:$seconds', |
|||
style: const TextStyle( |
|||
fontSize: 30, |
|||
fontWeight: FontWeight.bold, |
|||
color: Colors.white, |
|||
), |
|||
), |
|||
const SizedBox(width: 8), |
|||
const Icon( |
|||
Icons.timer, |
|||
color: Colors.white, |
|||
size: 30, |
|||
) |
|||
], |
|||
); |
|||
}, |
|||
); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue