chess_single_box.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import 'package:cchess/cchess.dart';
  2. import 'package:flutter/material.dart';
  3. import 'edit_fen.dart';
  4. import 'piece.dart';
  5. import '../global.dart';
  6. import '../models/game_manager.dart';
  7. import '../pages/home_page.dart';
  8. /// 棋子盒 单方
  9. class ChessSingleBox extends StatefulWidget {
  10. final String itemChrs;
  11. final String activeChr;
  12. final double width;
  13. final int team;
  14. const ChessSingleBox({
  15. Key? key,
  16. required this.itemChrs,
  17. this.activeChr = '',
  18. required this.width,
  19. this.team = 0,
  20. }) : super(key: key);
  21. @override
  22. State<ChessSingleBox> createState() => _ChessBoxState();
  23. }
  24. class _ChessBoxState extends State<ChessSingleBox> {
  25. static String allItemChrs = 'kabcnrp';
  26. GameManager? gamer;
  27. int matchCount(String chr) {
  28. return RegExp(chr).allMatches(widget.itemChrs).length;
  29. }
  30. void setActive(String chr) {
  31. EditFenState parent = context.findAncestorStateOfType<EditFenState>()!;
  32. parent.setActiveChr(chr);
  33. }
  34. void clearAll() {
  35. EditFenState parent = context.findAncestorStateOfType<EditFenState>()!;
  36. parent.clearAll();
  37. }
  38. @override
  39. void initState() {
  40. super.initState();
  41. if (widget.team == 0) {
  42. allItemChrs = allItemChrs.toUpperCase();
  43. }
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. if (gamer == null) {
  48. HomePageState gameWrapper =
  49. context.findAncestorStateOfType<HomePageState>()!;
  50. gamer = gameWrapper.gamer;
  51. }
  52. return SizedBox(
  53. width: widget.width,
  54. height: gamer!.skin.size * gamer!.scale,
  55. child: Flex(
  56. direction: Axis.horizontal,
  57. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  58. children: [
  59. Wrap(
  60. children: allItemChrs
  61. .split('')
  62. .map<Widget>(
  63. (String chr) => ItemWidget(
  64. chr: chr,
  65. count: matchCount(chr),
  66. isActive: widget.activeChr == chr,
  67. ),
  68. )
  69. .toList(),
  70. ),
  71. Wrap(
  72. children: [
  73. ElevatedButton(
  74. onPressed: () {
  75. clearAll();
  76. },
  77. child: Text(context.l10n.clearAll),
  78. )
  79. ],
  80. )
  81. ],
  82. ),
  83. );
  84. }
  85. }
  86. class ItemWidget extends StatelessWidget {
  87. final String chr;
  88. final int count;
  89. final bool isActive;
  90. const ItemWidget({
  91. Key? key,
  92. required this.chr,
  93. required this.count,
  94. this.isActive = false,
  95. }) : super(key: key);
  96. @override
  97. Widget build(BuildContext context) {
  98. HomePageState wrapper = context.findAncestorStateOfType<HomePageState>()!;
  99. GameManager manager = wrapper.gamer;
  100. _ChessBoxState parent = context.findAncestorStateOfType<_ChessBoxState>()!;
  101. return GestureDetector(
  102. onTap: () {
  103. if (count > 0) {
  104. parent.setActive(chr);
  105. }
  106. },
  107. child: SizedBox(
  108. width: manager.skin.size * manager.scale,
  109. height: manager.skin.size * manager.scale,
  110. child: Stack(
  111. children: [
  112. Piece(
  113. isActive: isActive,
  114. item: ChessItem(
  115. chr,
  116. ),
  117. ),
  118. Align(
  119. alignment: Alignment.topRight,
  120. child: Container(
  121. width: 20,
  122. height: 20,
  123. decoration: BoxDecoration(
  124. color: count > 0 ? Colors.red : Colors.grey,
  125. borderRadius: const BorderRadius.all(Radius.circular(10)),
  126. ),
  127. child: Center(
  128. child: Text(
  129. count.toString(),
  130. style: const TextStyle(color: Colors.white),
  131. ),
  132. ),
  133. ),
  134. )
  135. ],
  136. ),
  137. ),
  138. );
  139. }
  140. }