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.

147 lines
5.3 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
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/palette.dart';
  10. import 'package:my_flutter_puzzle/screens/photo/photo_screen_large.dart';
  11. import 'package:my_flutter_puzzle/utils/color_brightness.dart';
  12. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  13. import 'package:rive/rive.dart';
  14. class PhotoScreen extends ConsumerStatefulWidget {
  15. const PhotoScreen({
  16. required this.solverClient,
  17. required this.initialPuzzleData,
  18. required this.puzzleSize,
  19. required this.riveController,
  20. required this.duration,
  21. required this.level,
  22. Key? key,
  23. }) : super(key: key);
  24. final PuzzleSolverClient solverClient;
  25. final PuzzleData initialPuzzleData;
  26. final int duration;
  27. final int puzzleSize;
  28. final int level;
  29. final RiveAnimationController riveController;
  30. @override
  31. ConsumerState<ConsumerStatefulWidget> createState() => _PhotoScreenState();
  32. }
  33. class _PhotoScreenState extends ConsumerState<PhotoScreen> {
  34. late final PuzzleSolverClient _solverClient;
  35. late final int _puzzleSize;
  36. late final PuzzleData _initialPuzzleData;
  37. late final RiveAnimationController _riveController;
  38. late ThemeData _themeData = Theme.of(context);
  39. @override
  40. void initState() {
  41. _solverClient = widget.solverClient;
  42. _puzzleSize = widget.puzzleSize;
  43. _initialPuzzleData = widget.initialPuzzleData;
  44. _riveController = widget.riveController;
  45. super.initState();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. ref.listen(puzzleNotifierProvider(_solverClient),
  50. (previous, PuzzleState next) {
  51. if (next is PuzzleSolved) {
  52. BlocProvider.of<CountDownTimerCubit>(context).stop();
  53. }
  54. });
  55. ref.listen(imageSplitterNotifierProvider, (previous, next) {
  56. if (next is ImageSplitterComplete) {
  57. _themeData = Theme.of(context).copyWith(
  58. colorScheme: ColorScheme(
  59. brightness: Theme.of(context).brightness,
  60. primary: next.palette.vibrantColor?.color ?? Palette.blue,
  61. onPrimary: next.palette.lightVibrantColor?.color ?? Colors.white,
  62. secondary: Theme.of(context).colorScheme.secondary,
  63. onSecondary: Theme.of(context).colorScheme.onSecondary,
  64. error: Theme.of(context).colorScheme.error,
  65. onError: Theme.of(context).colorScheme.onError,
  66. background:
  67. next.palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  68. onBackground: Colors.white,
  69. surface: Theme.of(context).colorScheme.surface,
  70. onSurface: Theme.of(context).colorScheme.onSurface,
  71. ),
  72. );
  73. setState(() {});
  74. }
  75. });
  76. return Consumer(
  77. builder: (context, ref, child) {
  78. final state = ref.watch(imageSplitterNotifierProvider);
  79. return state.maybeWhen(
  80. () {
  81. return Theme(
  82. data: _themeData,
  83. child: PhotoScreenLarge(
  84. solverClient: _solverClient,
  85. level: widget.level,
  86. initialPuzzleData: _initialPuzzleData,
  87. duration: widget.duration,
  88. puzzleSize: _puzzleSize,
  89. riveController: _riveController,
  90. ),
  91. );
  92. },
  93. complete: (image, images, palette) {
  94. _themeData = Theme.of(context).copyWith(
  95. colorScheme: ColorScheme(
  96. brightness: Theme.of(context).brightness,
  97. primary: palette.vibrantColor?.color ?? Palette.blue,
  98. onPrimary: palette.lightVibrantColor?.color ?? Colors.white,
  99. secondary: Theme.of(context).colorScheme.secondary,
  100. onSecondary: Theme.of(context).colorScheme.onSecondary,
  101. error: Theme.of(context).colorScheme.error,
  102. onError: Theme.of(context).colorScheme.onError,
  103. background:
  104. palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  105. onBackground: Colors.white,
  106. surface: Theme.of(context).colorScheme.surface,
  107. onSurface: Theme.of(context).colorScheme.onSurface,
  108. ),
  109. );
  110. return Theme(
  111. data: _themeData,
  112. child: PhotoScreenLarge(
  113. solverClient: _solverClient,
  114. initialPuzzleData: _initialPuzzleData,
  115. puzzleSize: _puzzleSize,
  116. level: widget.level,
  117. riveController: _riveController,
  118. duration: widget.duration,
  119. ),
  120. );
  121. },
  122. orElse: () => Theme(
  123. data: _themeData,
  124. child: PhotoScreenLarge(
  125. solverClient: _solverClient,
  126. initialPuzzleData: _initialPuzzleData,
  127. duration: widget.duration,
  128. level: widget.level,
  129. puzzleSize: _puzzleSize,
  130. riveController: _riveController,
  131. ),
  132. ),
  133. );
  134. },
  135. );
  136. }
  137. }