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.

69 lines
2.2 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/models/puzzle_data.dart';
  5. import 'package:my_flutter_puzzle/providers.dart';
  6. import 'package:my_flutter_puzzle/screens/photo/photo_screen.dart';
  7. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  8. import 'package:rive/rive.dart';
  9. class PuzzleStarterScreen extends ConsumerStatefulWidget {
  10. final int puzzleSize;
  11. final String image;
  12. final int duration;
  13. final int level;
  14. const PuzzleStarterScreen({
  15. Key? key,
  16. required this.duration,
  17. required this.puzzleSize,
  18. required this.image,
  19. required this.level,
  20. }) : super(key: key);
  21. @override
  22. ConsumerState<ConsumerStatefulWidget> createState() => _SoloScreenState();
  23. }
  24. class _SoloScreenState extends ConsumerState<PuzzleStarterScreen> {
  25. late final PuzzleSolverClient _solverClient;
  26. late RiveAnimationController _riveController;
  27. late final int _puzzleSize;
  28. late final PuzzleData _initialPuzzleData;
  29. @override
  30. void initState() {
  31. _puzzleSize = widget.puzzleSize;
  32. _riveController = SimpleAnimation('idle');
  33. _solverClient = PuzzleSolverClient(size: _puzzleSize);
  34. _initialPuzzleData = ref.read(puzzleNotifierProvider(_solverClient).notifier).generateInitialPuzzle();
  35. ref.refresh(imageSplitterNotifierProvider);
  36. final state = ref.read(imageSplitterNotifierProvider);
  37. if (state is! ImageSplitterComplete) {
  38. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  39. ref.read(imageSplitterNotifierProvider.notifier).getInitialImages(
  40. puzzleSize: _puzzleSize,
  41. imagePath: widget.image,
  42. );
  43. });
  44. }
  45. ref.read(puzzleTypeNotifierProvider.notifier).changeToPhoto();
  46. super.initState();
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return AnimatedSwitcher(
  51. duration: const Duration(milliseconds: 400),
  52. child: PhotoScreen(
  53. solverClient: _solverClient,
  54. initialPuzzleData: _initialPuzzleData,
  55. level: widget.level,
  56. duration: widget.duration,
  57. puzzleSize: _puzzleSize,
  58. riveController: _riveController,
  59. ),
  60. );
  61. }
  62. }