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.

214 lines
7.6 KiB

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