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.

84 lines
2.7 KiB

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/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.read(puzzleNotifierProvider(_solverClient).notifier).initializePuzzle(
  30. initialPuzzleData: _initialPuzzleData,
  31. ),
  32. padding: padding,
  33. width: width,
  34. ),
  35. initializing: () => PuzzleGameButton(
  36. text: 'Get ready...',
  37. onTap: null,
  38. padding: padding,
  39. width: width,
  40. ),
  41. scrambling: (_) => PuzzleGameButton(
  42. text: 'Get ready...',
  43. onTap: null,
  44. padding: padding,
  45. width: width,
  46. ),
  47. current: (puzzleData) => const SizedBox(),
  48. computingSolution: (puzzleData) => PuzzleGameButton(
  49. text: 'Processing...',
  50. onTap: null,
  51. padding: padding,
  52. width: width,
  53. ),
  54. autoSolving: (puzzleData) => PuzzleGameButton(
  55. text: 'Solving...',
  56. onTap: null,
  57. padding: padding,
  58. width: width,
  59. ),
  60. solved: (puzzleData) => PuzzleGameButton(
  61. text: 'Start Game',
  62. onTap: () => ref.read(puzzleNotifierProvider(_solverClient).notifier).initializePuzzle(
  63. initialPuzzleData: puzzleData,
  64. ),
  65. padding: padding,
  66. width: width,
  67. ),
  68. error: (_) => PuzzleGameButton(
  69. text: 'Start Game',
  70. onTap: () => ref.read(puzzleNotifierProvider(_solverClient).notifier).initializePuzzle(
  71. initialPuzzleData: _initialPuzzleData,
  72. ),
  73. padding: padding,
  74. width: width,
  75. ),
  76. );
  77. },
  78. );
  79. }
  80. }