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.

58 lines
1.9 KiB

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/puzzle_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. const PuzzleStarterScreen({
  14. Key? key,
  15. required this.duration,
  16. required this.puzzleSize,
  17. required this.image,
  18. }) : super(key: key);
  19. @override
  20. ConsumerState<ConsumerStatefulWidget> createState() => _SoloScreenState();
  21. }
  22. class _SoloScreenState extends ConsumerState<PuzzleStarterScreen> {
  23. late final PuzzleSolverClient _solverClient;
  24. late RiveAnimationController _riveController;
  25. late final int _puzzleSize;
  26. late final PuzzleData _initialPuzzleData;
  27. @override
  28. void initState() {
  29. _puzzleSize = widget.puzzleSize;
  30. _riveController = SimpleAnimation('idle');
  31. _solverClient = PuzzleSolverClient(size: _puzzleSize);
  32. _initialPuzzleData = ref.read(puzzleNotifierProvider(_solverClient).notifier).generateInitialPuzzle();
  33. super.initState();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. ref.listen(puzzleNotifierProvider(_solverClient), (previous, PuzzleState next) {
  38. if (next is PuzzleSolved) {
  39. ref.read(timerNotifierProvider.notifier).stopTimer();
  40. }
  41. });
  42. return AnimatedSwitcher(
  43. duration: const Duration(milliseconds: 400),
  44. child: PhotoScreen(
  45. solverClient: _solverClient,
  46. initialPuzzleData: _initialPuzzleData,
  47. puzzleSize: _puzzleSize,
  48. riveController: _riveController,
  49. ),
  50. );
  51. }
  52. }