You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:my_flutter_puzzle/application/states/image_splitter_state.dart';
  5. import 'package:my_flutter_puzzle/application/states/puzzle_state.dart';
  6. import 'package:my_flutter_puzzle/cubits/count_down_timer_cubit.dart';
  7. import 'package:my_flutter_puzzle/models/puzzle_data.dart';
  8. import 'package:my_flutter_puzzle/providers.dart';
  9. import 'package:my_flutter_puzzle/res/puzzle_constants.dart';
  10. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  11. import 'package:my_flutter_puzzle/utils/utils.dart';
  12. import 'package:my_flutter_puzzle/widgets/photo_screen/image_viewer.dart';
  13. import 'package:my_flutter_puzzle/widgets/solo_screen/count_down_timer_widget.dart';
  14. import 'package:my_flutter_puzzle/widgets/solo_screen/solo_screen_export.dart';
  15. import 'package:palette_generator/palette_generator.dart';
  16. import 'package:rive/rive.dart';
  17. class PhotoScreenLarge extends ConsumerStatefulWidget {
  18. const PhotoScreenLarge({
  19. required this.solverClient,
  20. required this.initialPuzzleData,
  21. required this.puzzleSize,
  22. required this.riveController,
  23. required this.duration,
  24. Key? key,
  25. }) : super(key: key);
  26. final PuzzleSolverClient solverClient;
  27. final PuzzleData initialPuzzleData;
  28. final int puzzleSize;
  29. final int duration;
  30. final RiveAnimationController riveController;
  31. @override
  32. ConsumerState<ConsumerStatefulWidget> createState() => _SoloScreenLargeState();
  33. }
  34. class _SoloScreenLargeState extends ConsumerState<PhotoScreenLarge> {
  35. late final PuzzleSolverClient _solverClient;
  36. late final int _puzzleSize;
  37. late final PuzzleData _initialPuzzleData;
  38. late final RiveAnimationController _riveController;
  39. bool _puzzleSolved = false;
  40. bool _isStartPressed = false;
  41. final double _fontSize = 70.0;
  42. final double _boardSize = 280.0;
  43. final int _spacing = 4;
  44. late double _eachBoxSize;
  45. List<Image>? _previousImages;
  46. Image? _previousImage;
  47. PaletteGenerator? _previousPalette;
  48. @override
  49. void initState() {
  50. _solverClient = widget.solverClient;
  51. _puzzleSize = widget.puzzleSize;
  52. _eachBoxSize = (_boardSize / _puzzleSize) - (_spacing * (_puzzleSize - 1));
  53. _initialPuzzleData = widget.initialPuzzleData;
  54. _riveController = widget.riveController;
  55. super.initState();
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. ref.listen(puzzleNotifierProvider(_solverClient), (previous, PuzzleState next) {
  60. if (next is PuzzleInitializing) {
  61. setState(() {
  62. _isStartPressed = true;
  63. });
  64. }
  65. if (next is PuzzleSolved) {
  66. _puzzleSolved = true;
  67. }
  68. });
  69. ref.listen(imageSplitterNotifierProvider, (previous, next) {
  70. if (next is ImageSplitterComplete) {
  71. setState(() {
  72. _previousImages = next.images;
  73. _previousImage = next.image;
  74. _previousPalette = next.palette;
  75. });
  76. ref.read(puzzleNotifierProvider(_solverClient).notifier).initializePuzzle(
  77. initialPuzzleData: _initialPuzzleData,
  78. );
  79. }
  80. });
  81. return WillPopScope(
  82. onWillPop: () async {
  83. BlocProvider.of<CountDownTimerCubit>(context).stop();
  84. return true;
  85. },
  86. child: Scaffold(
  87. backgroundColor: Theme.of(context).colorScheme.background,
  88. body: SafeArea(
  89. child: Row(
  90. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  91. children: [
  92. Padding(
  93. padding: const EdgeInsetsDirectional.symmetric(horizontal: 10),
  94. child: Column(
  95. mainAxisAlignment: MainAxisAlignment.center,
  96. crossAxisAlignment: CrossAxisAlignment.center,
  97. children: [
  98. Visibility(
  99. visible: !_isStartPressed,
  100. child: ImageViewer(
  101. puzzleSize: _puzzleSize,
  102. previousImage: _previousImage,
  103. previousPalette: _previousPalette,
  104. imageSize: 200,
  105. ),
  106. ),
  107. const SizedBox(height: 32),
  108. MovesTilesWidget(solverClient: _solverClient, fontSize: 16),
  109. const SizedBox(height: 32),
  110. ],
  111. ),
  112. ),
  113. SingleChildScrollView(
  114. child: Column(
  115. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  116. children: [
  117. CountDownTimerWidget(
  118. duration: widget.duration,
  119. finishCallback: () {
  120. if (!_puzzleSolved) {
  121. Utils.instance.showToast(context, 'TimeOut');
  122. Future.delayed(const Duration(milliseconds: 1500), () {
  123. Navigator.pop(context);
  124. });
  125. }
  126. },
  127. ),
  128. const SizedBox(height: 12),
  129. Consumer(
  130. builder: (context, ref, child) {
  131. final state = ref.watch(imageSplitterNotifierProvider);
  132. return state.maybeWhen(
  133. () => PuzzleWidget(
  134. solverClient: _solverClient,
  135. boardSize: _boardSize,
  136. eachBoxSize: _eachBoxSize,
  137. initialPuzzleData: _initialPuzzleData,
  138. fontSize: _fontSize,
  139. images: _previousImages,
  140. kInitialSpeed: kInitialSpeed,
  141. ),
  142. complete: (image, images, palette) {
  143. _previousImages = images;
  144. _previousImage = image;
  145. _previousPalette = palette;
  146. return PuzzleWidget(
  147. solverClient: _solverClient,
  148. boardSize: _boardSize,
  149. eachBoxSize: _eachBoxSize,
  150. initialPuzzleData: _initialPuzzleData,
  151. fontSize: _fontSize,
  152. images: images,
  153. kInitialSpeed: kInitialSpeed,
  154. );
  155. },
  156. orElse: () => PuzzleWidget(
  157. solverClient: _solverClient,
  158. boardSize: _boardSize,
  159. eachBoxSize: _eachBoxSize,
  160. initialPuzzleData: _initialPuzzleData,
  161. fontSize: _fontSize,
  162. images: _previousImages,
  163. kInitialSpeed: kInitialSpeed,
  164. ),
  165. );
  166. },
  167. ),
  168. const SizedBox(height: 30),
  169. ],
  170. ),
  171. ),
  172. Padding(
  173. padding: const EdgeInsetsDirectional.symmetric(horizontal: 10),
  174. child: Stack(
  175. children: [
  176. AnimatedDash(
  177. boardSize: _boardSize * 0.8,
  178. riveController: _riveController,
  179. onInit: (_) => setState(() {}),
  180. ),
  181. Column(
  182. mainAxisSize: MainAxisSize.max,
  183. crossAxisAlignment: CrossAxisAlignment.end,
  184. mainAxisAlignment: MainAxisAlignment.center,
  185. children: [
  186. CountdownWidget(
  187. isStartPressed: _isStartPressed,
  188. onFinish: () {
  189. BlocProvider.of<CountDownTimerCubit>(context).start();
  190. setState(() {
  191. _isStartPressed = false;
  192. });
  193. },
  194. initialSpeed: kInitialSpeed,
  195. ),
  196. ],
  197. ),
  198. ],
  199. ),
  200. ),
  201. ],
  202. ),
  203. ),
  204. ),
  205. );
  206. }
  207. }