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.

124 lines
3.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/models/puzzle_data.dart';
  4. import 'package:my_flutter_puzzle/providers.dart';
  5. import 'package:my_flutter_puzzle/utils/puzzle_solver.dart';
  6. import 'puzzle_widget/puzzle_board.dart';
  7. class PuzzleWidget extends StatelessWidget {
  8. const PuzzleWidget({
  9. Key? key,
  10. required PuzzleSolverClient solverClient,
  11. required this.boardSize,
  12. required this.eachBoxSize,
  13. required PuzzleData initialPuzzleData,
  14. required this.fontSize,
  15. this.images,
  16. this.borderRadius = 20,
  17. required int kInitialSpeed,
  18. }) : _solverClient = solverClient,
  19. _initialPuzzleData = initialPuzzleData,
  20. _kInitialSpeed = kInitialSpeed,
  21. super(key: key);
  22. final PuzzleSolverClient _solverClient;
  23. final double boardSize;
  24. final double eachBoxSize;
  25. final PuzzleData _initialPuzzleData;
  26. final double fontSize;
  27. final List<Image>? images;
  28. final int _kInitialSpeed;
  29. final double borderRadius;
  30. @override
  31. Widget build(BuildContext context) {
  32. return Consumer(
  33. builder: (context, ref, child) {
  34. final state = ref.watch(puzzleNotifierProvider(_solverClient));
  35. return state.when(
  36. () => PuzzleBoard(
  37. solverClient: _solverClient,
  38. boardSize: boardSize,
  39. eachBoxSize: eachBoxSize,
  40. puzzleData: _initialPuzzleData,
  41. fontSize: fontSize,
  42. images: images,
  43. isEnabled: false,
  44. animationSpeed: _kInitialSpeed,
  45. borderRadius: borderRadius,
  46. ),
  47. initializing: () => PuzzleBoard(
  48. solverClient: _solverClient,
  49. boardSize: boardSize,
  50. eachBoxSize: eachBoxSize,
  51. puzzleData: _initialPuzzleData,
  52. fontSize: fontSize,
  53. images: images,
  54. isEnabled: false,
  55. animationSpeed: _kInitialSpeed,
  56. borderRadius: borderRadius,
  57. ),
  58. scrambling: (puzzleData) => PuzzleBoard(
  59. solverClient: _solverClient,
  60. boardSize: boardSize,
  61. eachBoxSize: eachBoxSize,
  62. puzzleData: puzzleData,
  63. fontSize: fontSize,
  64. images: images,
  65. isEnabled: false,
  66. animationSpeed: _kInitialSpeed,
  67. borderRadius: borderRadius,
  68. ),
  69. current: (puzzleData) => PuzzleBoard(
  70. solverClient: _solverClient,
  71. boardSize: boardSize,
  72. eachBoxSize: eachBoxSize,
  73. puzzleData: puzzleData,
  74. fontSize: fontSize,
  75. images: images,
  76. borderRadius: borderRadius,
  77. ),
  78. computingSolution: (puzzleData) => PuzzleBoard(
  79. solverClient: _solverClient,
  80. boardSize: boardSize,
  81. eachBoxSize: eachBoxSize,
  82. puzzleData: puzzleData,
  83. fontSize: fontSize,
  84. images: images,
  85. borderRadius: borderRadius,
  86. ),
  87. autoSolving: (puzzleData) => PuzzleBoard(
  88. solverClient: _solverClient,
  89. boardSize: boardSize,
  90. eachBoxSize: eachBoxSize,
  91. puzzleData: puzzleData,
  92. fontSize: fontSize,
  93. images: images,
  94. borderRadius: borderRadius,
  95. ),
  96. solved: (puzzleData) => PuzzleBoard(
  97. solverClient: _solverClient,
  98. boardSize: boardSize,
  99. eachBoxSize: eachBoxSize,
  100. puzzleData: puzzleData,
  101. fontSize: fontSize,
  102. images: images,
  103. isEnabled: false,
  104. borderRadius: borderRadius,
  105. ),
  106. error: (_) => PuzzleBoard(
  107. solverClient: _solverClient,
  108. boardSize: boardSize,
  109. eachBoxSize: eachBoxSize,
  110. puzzleData: _initialPuzzleData,
  111. fontSize: fontSize,
  112. images: images,
  113. borderRadius: borderRadius,
  114. ),
  115. );
  116. },
  117. );
  118. }
  119. }