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.

148 lines
4.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/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 SoloScreenMedium extends ConsumerStatefulWidget {
  13. const SoloScreenMedium({
  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<SoloScreenMedium> {
  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 = 64.0;
  58. var boardSize = 400.0;
  59. var spacing = 5;
  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. ),
  75. ],
  76. ),
  77. SingleChildScrollView(
  78. child: Column(
  79. mainAxisAlignment: MainAxisAlignment.center,
  80. // crossAxisAlignment: CrossAxisAlignment.start,
  81. children: [
  82. Row(),
  83. // Text(
  84. // _puzzleType,
  85. // style: const TextStyle(
  86. // fontSize: 18,
  87. // fontWeight: FontWeight.w500,
  88. // color: Colors.white,
  89. // ),
  90. // ),
  91. const SizedBox(height: 8),
  92. const Text(
  93. 'Puzzle Challenge',
  94. style: TextStyle(
  95. fontSize: 36,
  96. fontWeight: FontWeight.w500,
  97. color: Colors.white,
  98. ),
  99. ),
  100. const SizedBox(height: 16),
  101. MovesTilesWidget(solverClient: _solverClient),
  102. const SizedBox(height: 16),
  103. const TimerWidget(fontSize: 36),
  104. const SizedBox(height: 36),
  105. PuzzleWidget(
  106. solverClient: _solverClient,
  107. boardSize: boardSize,
  108. eachBoxSize: eachBoxSize,
  109. initialPuzzleData: _initialPuzzleData,
  110. fontSize: fontSize,
  111. kInitialSpeed: kInitialSpeed,
  112. ),
  113. const SizedBox(height: 36),
  114. GameButtonWidget(
  115. solverClient: _solverClient,
  116. initialPuzzleData: _initialPuzzleData,
  117. ),
  118. const SizedBox(height: 100),
  119. ],
  120. ),
  121. ),
  122. ],
  123. ),
  124. ),
  125. CountdownOverlay(
  126. isStartPressed: _isStartPressed,
  127. onFinish: () {
  128. ref.read(timerNotifierProvider.notifier).startTimer();
  129. setState(() {
  130. _isStartPressed = false;
  131. });
  132. },
  133. initialSpeed: kInitialSpeed,
  134. ),
  135. ],
  136. );
  137. }
  138. }