chess_box.dart 4.0 KB

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