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.

145 lines
4.8 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/providers.dart';
  5. import 'package:my_flutter_puzzle/res/puzzle_constants.dart';
  6. import 'package:my_flutter_puzzle/screens/puzzle/top_bar.dart';
  7. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  8. import 'package:my_flutter_puzzle/widgets/solo_screen/countdown_overlay.dart';
  9. import 'package:my_flutter_puzzle/widgets/solo_screen/solo_screen_export.dart';
  10. import 'package:rive/rive.dart';
  11. import '../../models/puzzle_data.dart';
  12. class SoloScreenSmall extends ConsumerStatefulWidget {
  13. const SoloScreenSmall({
  14. required this.solverClient,
  15. required this.initialPuzzleData,
  16. required this.puzzleSize,
  17. required this.puzzleType,
  18. required this.riveController,
  19. Key? key,
  20. }) : super(key: key);
  21. final PuzzleSolverClient solverClient;
  22. final PuzzleData initialPuzzleData;
  23. final int puzzleSize;
  24. final String puzzleType;
  25. final RiveAnimationController riveController;
  26. @override
  27. ConsumerState<ConsumerStatefulWidget> createState() => _SoloScreenLargeState();
  28. }
  29. class _SoloScreenLargeState extends ConsumerState<SoloScreenSmall> {
  30. late final PuzzleSolverClient _solverClient;
  31. late final int _puzzleSize;
  32. late final PuzzleData _initialPuzzleData;
  33. late final String _puzzleType;
  34. late final RiveAnimationController _riveController;
  35. bool _isStartPressed = false;
  36. @override
  37. void initState() {
  38. _solverClient = widget.solverClient;
  39. _puzzleSize = widget.puzzleSize;
  40. _initialPuzzleData = widget.initialPuzzleData;
  41. _puzzleType = widget.puzzleType;
  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. // TODO: Add celebration
  50. }
  51. if (next is PuzzleInitializing) {
  52. setState(() {
  53. _isStartPressed = true;
  54. });
  55. }
  56. });
  57. var fontSize = 48.0;
  58. var boardSize = 300.0;
  59. var spacing = 3;
  60. var eachBoxSize = (boardSize / _puzzleSize) - (spacing * (_puzzleSize - 1));
  61. return Stack(
  62. children: [
  63. Scaffold(
  64. backgroundColor: Theme.of(context).backgroundColor,
  65. body: Stack(
  66. children: [
  67. Row(
  68. children: [
  69. const Spacer(),
  70. AnimatedDash(
  71. boardSize: boardSize / 1.5,
  72. riveController: _riveController,
  73. onInit: (_) => setState(() {}),
  74. padding: const EdgeInsets.only(right: 16.0, bottom: 30),
  75. ),
  76. ],
  77. ),
  78. SingleChildScrollView(
  79. child: Column(
  80. mainAxisAlignment: MainAxisAlignment.center,
  81. children: [
  82. Row(),
  83. const SizedBox(height: 30),
  84. const Text(
  85. 'Puzzle Challenge',
  86. style: TextStyle(
  87. fontSize: 32,
  88. fontWeight: FontWeight.w500,
  89. color: Colors.white,
  90. ),
  91. ),
  92. const SizedBox(height: 8),
  93. MovesTilesWidget(
  94. solverClient: _solverClient,
  95. fontSize: 22,
  96. ),
  97. const SizedBox(height: 8),
  98. const TimerWidget(fontSize: 24),
  99. PuzzleWidget(
  100. solverClient: _solverClient,
  101. boardSize: boardSize,
  102. eachBoxSize: eachBoxSize,
  103. initialPuzzleData: _initialPuzzleData,
  104. fontSize: fontSize,
  105. kInitialSpeed: kInitialSpeed,
  106. borderRadius: 16,
  107. ),
  108. const SizedBox(height: 24),
  109. GameButtonWidget(
  110. solverClient: _solverClient,
  111. initialPuzzleData: _initialPuzzleData,
  112. padding: const EdgeInsets.only(top: 10.0, bottom: 9.0),
  113. width: 130,
  114. ),
  115. const SizedBox(height: 100),
  116. ],
  117. ),
  118. ),
  119. ],
  120. ),
  121. ),
  122. CountdownOverlay(
  123. isStartPressed: _isStartPressed,
  124. onFinish: () {
  125. ref.read(timerNotifierProvider.notifier).startTimer();
  126. setState(() {
  127. _isStartPressed = false;
  128. });
  129. },
  130. initialSpeed: kInitialSpeed,
  131. ),
  132. ],
  133. );
  134. }
  135. }