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.

175 lines
6.7 KiB

2 years ago
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:my_flutter_puzzle/application/states/image_splitter_state.dart';
  4. import 'package:my_flutter_puzzle/application/states/puzzle_state.dart';
  5. import 'package:my_flutter_puzzle/models/puzzle_data.dart';
  6. import 'package:my_flutter_puzzle/providers.dart';
  7. import 'package:my_flutter_puzzle/res/palette.dart';
  8. import 'package:my_flutter_puzzle/screens/photo/photo_screen_large.dart';
  9. import 'package:my_flutter_puzzle/screens/photo/photo_screen_medium.dart';
  10. import 'package:my_flutter_puzzle/screens/photo/photo_screen_small.dart';
  11. import 'package:my_flutter_puzzle/utils/color_brightness.dart';
  12. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  13. import 'package:my_flutter_puzzle/utils/responsive_layout.dart';
  14. import 'package:rive/rive.dart';
  15. class PhotoScreen extends ConsumerStatefulWidget {
  16. const PhotoScreen({
  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() => _PhotoScreenState();
  29. }
  30. class _PhotoScreenState extends ConsumerState<PhotoScreen> {
  31. late final PuzzleSolverClient _solverClient;
  32. late final int _puzzleSize;
  33. late final PuzzleData _initialPuzzleData;
  34. late final RiveAnimationController _riveController;
  35. late ThemeData _themeData = Theme.of(context);
  36. @override
  37. void initState() {
  38. _solverClient = widget.solverClient;
  39. _puzzleSize = widget.puzzleSize;
  40. _initialPuzzleData = widget.initialPuzzleData;
  41. _riveController = widget.riveController;
  42. super.initState();
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. ref.listen(puzzleNotifierProvider(_solverClient), (previous, PuzzleState next) {
  47. if (next is PuzzleSolved) {
  48. ref.read(timerNotifierProvider.notifier).stopTimer();
  49. }
  50. });
  51. ref.listen(imageSplitterNotifierProvider, (previous, next) {
  52. if (next is ImageSplitterComplete) {
  53. _themeData = Theme.of(context).copyWith(
  54. colorScheme: ColorScheme(
  55. brightness: Theme.of(context).brightness,
  56. primary: next.palette.vibrantColor?.color ?? Palette.blue,
  57. onPrimary: next.palette.lightVibrantColor?.color ?? Colors.white,
  58. secondary: Theme.of(context).colorScheme.secondary,
  59. onSecondary: Theme.of(context).colorScheme.onSecondary,
  60. error: Theme.of(context).colorScheme.error,
  61. onError: Theme.of(context).colorScheme.onError,
  62. background: next.palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  63. onBackground: Colors.white,
  64. surface: Theme.of(context).colorScheme.surface,
  65. onSurface: Theme.of(context).colorScheme.onSurface,
  66. ),
  67. );
  68. setState(() {});
  69. }
  70. });
  71. return Consumer(
  72. builder: (context, ref, child) {
  73. final state = ref.watch(imageSplitterNotifierProvider);
  74. return state.maybeWhen(
  75. () => Theme(
  76. data: _themeData,
  77. child: ResponsiveLayout(
  78. largeChild: PhotoScreenLarge(
  79. solverClient: _solverClient,
  80. initialPuzzleData: _initialPuzzleData,
  81. puzzleSize: _puzzleSize,
  82. riveController: _riveController,
  83. ),
  84. mediumChild: PhotoScreenMedium(
  85. solverClient: _solverClient,
  86. initialPuzzleData: _initialPuzzleData,
  87. puzzleSize: _puzzleSize,
  88. riveController: _riveController,
  89. ),
  90. smallChild: PhotoScreenSmall(
  91. solverClient: _solverClient,
  92. initialPuzzleData: _initialPuzzleData,
  93. puzzleSize: _puzzleSize,
  94. riveController: _riveController,
  95. ),
  96. ),
  97. ),
  98. complete: (image, images, palette) {
  99. _themeData = Theme.of(context).copyWith(
  100. colorScheme: ColorScheme(
  101. brightness: Theme.of(context).brightness,
  102. primary: palette.vibrantColor?.color ?? Palette.blue,
  103. onPrimary: palette.lightVibrantColor?.color ?? Colors.white,
  104. secondary: Theme.of(context).colorScheme.secondary,
  105. onSecondary: Theme.of(context).colorScheme.onSecondary,
  106. error: Theme.of(context).colorScheme.error,
  107. onError: Theme.of(context).colorScheme.onError,
  108. background: palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  109. onBackground: Colors.white,
  110. surface: Theme.of(context).colorScheme.surface,
  111. onSurface: Theme.of(context).colorScheme.onSurface,
  112. ),
  113. );
  114. return Theme(
  115. data: _themeData,
  116. child: ResponsiveLayout(
  117. largeChild: PhotoScreenLarge(
  118. solverClient: _solverClient,
  119. initialPuzzleData: _initialPuzzleData,
  120. puzzleSize: _puzzleSize,
  121. riveController: _riveController,
  122. ),
  123. mediumChild: PhotoScreenMedium(
  124. solverClient: _solverClient,
  125. initialPuzzleData: _initialPuzzleData,
  126. puzzleSize: _puzzleSize,
  127. riveController: _riveController,
  128. ),
  129. smallChild: PhotoScreenSmall(
  130. solverClient: _solverClient,
  131. initialPuzzleData: _initialPuzzleData,
  132. puzzleSize: _puzzleSize,
  133. riveController: _riveController,
  134. ),
  135. ),
  136. );
  137. },
  138. orElse: () => Theme(
  139. data: _themeData,
  140. child: ResponsiveLayout(
  141. largeChild: PhotoScreenLarge(
  142. solverClient: _solverClient,
  143. initialPuzzleData: _initialPuzzleData,
  144. puzzleSize: _puzzleSize,
  145. riveController: _riveController,
  146. ),
  147. mediumChild: PhotoScreenMedium(
  148. solverClient: _solverClient,
  149. initialPuzzleData: _initialPuzzleData,
  150. puzzleSize: _puzzleSize,
  151. riveController: _riveController,
  152. ),
  153. smallChild: PhotoScreenSmall(
  154. solverClient: _solverClient,
  155. initialPuzzleData: _initialPuzzleData,
  156. puzzleSize: _puzzleSize,
  157. riveController: _riveController,
  158. ),
  159. ),
  160. ),
  161. );
  162. },
  163. );
  164. }
  165. }