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.

144 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), (previous, PuzzleState next) {
  50. if (next is PuzzleSolved) {
  51. BlocProvider.of<CountDownTimerCubit>(context).stop();
  52. }
  53. });
  54. ref.listen(imageSplitterNotifierProvider, (previous, next) {
  55. if (next is ImageSplitterComplete) {
  56. _themeData = Theme.of(context).copyWith(
  57. colorScheme: ColorScheme(
  58. brightness: Theme.of(context).brightness,
  59. primary: next.palette.vibrantColor?.color ?? Palette.blue,
  60. onPrimary: next.palette.lightVibrantColor?.color ?? Colors.white,
  61. secondary: Theme.of(context).colorScheme.secondary,
  62. onSecondary: Theme.of(context).colorScheme.onSecondary,
  63. error: Theme.of(context).colorScheme.error,
  64. onError: Theme.of(context).colorScheme.onError,
  65. background: next.palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  66. onBackground: Colors.white,
  67. surface: Theme.of(context).colorScheme.surface,
  68. onSurface: Theme.of(context).colorScheme.onSurface,
  69. ),
  70. );
  71. setState(() {});
  72. }
  73. });
  74. return Consumer(
  75. builder: (context, ref, child) {
  76. final state = ref.watch(imageSplitterNotifierProvider);
  77. return state.maybeWhen(
  78. () {
  79. return Theme(
  80. data: _themeData,
  81. child: PhotoScreenLarge(
  82. solverClient: _solverClient,
  83. level: widget.level,
  84. initialPuzzleData: _initialPuzzleData,
  85. duration: widget.duration,
  86. puzzleSize: _puzzleSize,
  87. riveController: _riveController,
  88. ),
  89. );
  90. },
  91. complete: (image, images, palette) {
  92. _themeData = Theme.of(context).copyWith(
  93. colorScheme: ColorScheme(
  94. brightness: Theme.of(context).brightness,
  95. primary: palette.vibrantColor?.color ?? Palette.blue,
  96. onPrimary: palette.lightVibrantColor?.color ?? Colors.white,
  97. secondary: Theme.of(context).colorScheme.secondary,
  98. onSecondary: Theme.of(context).colorScheme.onSecondary,
  99. error: Theme.of(context).colorScheme.error,
  100. onError: Theme.of(context).colorScheme.onError,
  101. background: palette.darkMutedColor?.color ?? Palette.blue.darken(0.3),
  102. onBackground: Colors.white,
  103. surface: Theme.of(context).colorScheme.surface,
  104. onSurface: Theme.of(context).colorScheme.onSurface,
  105. ),
  106. );
  107. return Theme(
  108. data: _themeData,
  109. child: PhotoScreenLarge(
  110. solverClient: _solverClient,
  111. initialPuzzleData: _initialPuzzleData,
  112. puzzleSize: _puzzleSize,
  113. level: widget.level,
  114. riveController: _riveController,
  115. duration: widget.duration,
  116. ),
  117. );
  118. },
  119. orElse: () => Theme(
  120. data: _themeData,
  121. child: PhotoScreenLarge(
  122. solverClient: _solverClient,
  123. initialPuzzleData: _initialPuzzleData,
  124. duration: widget.duration,
  125. level: widget.level,
  126. puzzleSize: _puzzleSize,
  127. riveController: _riveController,
  128. ),
  129. ),
  130. );
  131. },
  132. );
  133. }
  134. }