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.

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