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.

99 lines
4.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
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. class PuzzleBoard extends ConsumerWidget {
  7. const PuzzleBoard({
  8. Key? key,
  9. required this.solverClient,
  10. required this.boardSize,
  11. required this.eachBoxSize,
  12. required this.puzzleData,
  13. required this.fontSize,
  14. this.images,
  15. this.animationSpeed = 300,
  16. this.isEnabled = true,
  17. this.borderRadius = 20,
  18. }) : super(key: key);
  19. final PuzzleSolverClient solverClient;
  20. final double boardSize;
  21. final double eachBoxSize;
  22. final double fontSize;
  23. final PuzzleData puzzleData;
  24. final bool isEnabled;
  25. final int animationSpeed;
  26. final List<Image>? images;
  27. final double borderRadius;
  28. // final int _animationSpeedInMilliseconds = 300;
  29. @override
  30. Widget build(BuildContext context, WidgetRef ref) {
  31. return SizedBox(
  32. height: boardSize,
  33. width: boardSize,
  34. child: Stack(
  35. children: [
  36. for (int i = 0; i < puzzleData.offsetMap.length; i++)
  37. puzzleData.offsetMap.entries.toList()[i].key != 0
  38. ? AnimatedAlign(
  39. alignment: puzzleData.offsetMap.entries.toList()[i].value,
  40. duration: Duration(
  41. milliseconds: animationSpeed,
  42. ),
  43. curve: Curves.easeOut,
  44. child: MouseRegion(
  45. cursor: !isEnabled ? SystemMouseCursors.forbidden : SystemMouseCursors.click,
  46. child: GestureDetector(
  47. onTap: !isEnabled
  48. ? null
  49. : () => ref.read(puzzleNotifierProvider(solverClient).notifier).onClick(
  50. index: puzzleData.board1D.indexOf(puzzleData.offsetMap.entries.toList()[i].key),
  51. prev: puzzleData,
  52. ),
  53. child: images == null
  54. ? Card(
  55. elevation: 4,
  56. color: Theme.of(context).colorScheme.primary.withOpacity(isEnabled ? 1 : 0.5),
  57. shape: RoundedRectangleBorder(
  58. borderRadius: BorderRadius.circular(8),
  59. ),
  60. child: SizedBox(
  61. height: eachBoxSize,
  62. width: eachBoxSize,
  63. child: Center(
  64. child: Text(
  65. puzzleData.offsetMap.entries.toList()[i].key.toString(),
  66. style: TextStyle(
  67. fontSize: 30,
  68. fontWeight: FontWeight.bold,
  69. color: Colors.white.withOpacity(isEnabled ? 1 : 0.5),
  70. ),
  71. ),
  72. ),
  73. ),
  74. )
  75. : SizedBox(
  76. height: eachBoxSize,
  77. width: eachBoxSize,
  78. child: Opacity(
  79. opacity: isEnabled ? 1 : 0.5,
  80. child: ClipRRect(
  81. borderRadius: BorderRadius.circular(8),
  82. child:
  83. images![int.parse(puzzleData.offsetMap.entries.toList()[i].key.toString()) - 1],
  84. ),
  85. ),
  86. ),
  87. ),
  88. ),
  89. )
  90. : const SizedBox(),
  91. ],
  92. ),
  93. );
  94. }
  95. }