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.

101 lines
3.2 KiB

2 years ago
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:my_flutter_puzzle/models/puzzle_data.dart';
  4. import 'package:my_flutter_puzzle/providers.dart';
  5. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  6. import 'game_button_widget/puzzle_game_button.dart';
  7. class GameButtonWidget extends StatelessWidget {
  8. const GameButtonWidget({
  9. Key? key,
  10. required PuzzleSolverClient solverClient,
  11. required PuzzleData initialPuzzleData,
  12. this.width = 145,
  13. this.padding = const EdgeInsets.only(top: 13.0, bottom: 12.0),
  14. }) : _solverClient = solverClient,
  15. _initialPuzzleData = initialPuzzleData,
  16. super(key: key);
  17. final PuzzleSolverClient _solverClient;
  18. final PuzzleData _initialPuzzleData;
  19. final double width;
  20. final EdgeInsets padding;
  21. @override
  22. Widget build(BuildContext context) {
  23. return Consumer(
  24. builder: (context, ref, child) {
  25. final state = ref.watch(puzzleNotifierProvider(_solverClient));
  26. return state.when(
  27. () => PuzzleGameButton(
  28. text: 'Start Game',
  29. onTap: () => ref
  30. .read(puzzleNotifierProvider(_solverClient).notifier)
  31. .initializePuzzle(
  32. initialPuzzleData: _initialPuzzleData,
  33. ),
  34. padding: padding,
  35. width: width,
  36. ),
  37. initializing: () => PuzzleGameButton(
  38. text: 'Get ready...',
  39. onTap: null,
  40. padding: padding,
  41. width: width,
  42. ),
  43. scrambling: (_) => PuzzleGameButton(
  44. text: 'Get ready...',
  45. onTap: null,
  46. padding: padding,
  47. width: width,
  48. ),
  49. current: (puzzleData) => PuzzleGameButton(
  50. text: 'Restart',
  51. onTap: () {
  52. ref.read(timerNotifierProvider.notifier).stopTimer();
  53. ref
  54. .read(puzzleNotifierProvider(_solverClient).notifier)
  55. .restartPuzzle();
  56. },
  57. padding: padding,
  58. width: width,
  59. ),
  60. computingSolution: (puzzleData) => PuzzleGameButton(
  61. text: 'Processing...',
  62. onTap: null,
  63. padding: padding,
  64. width: width,
  65. ),
  66. autoSolving: (puzzleData) => PuzzleGameButton(
  67. text: 'Solving...',
  68. onTap: null,
  69. padding: padding,
  70. width: width,
  71. ),
  72. solved: (puzzleData) => PuzzleGameButton(
  73. text: 'Start Game',
  74. onTap: () => ref
  75. .read(puzzleNotifierProvider(_solverClient).notifier)
  76. .initializePuzzle(
  77. initialPuzzleData: puzzleData,
  78. ),
  79. padding: padding,
  80. width: width,
  81. ),
  82. error: (_) => PuzzleGameButton(
  83. text: 'Start Game',
  84. onTap: () => ref
  85. .read(puzzleNotifierProvider(_solverClient).notifier)
  86. .initializePuzzle(
  87. initialPuzzleData: _initialPuzzleData,
  88. ),
  89. padding: padding,
  90. width: width,
  91. ),
  92. );
  93. },
  94. );
  95. }
  96. }