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.

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