123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import 'package:cchess/cchess.dart';
- import 'package:flutter/material.dart';
- import 'edit_fen.dart';
- import 'piece.dart';
- import '../global.dart';
- import '../models/game_manager.dart';
- import '../pages/home_page.dart';
- /// 棋子盒 双方
- class ChessBox extends StatefulWidget {
- final String itemChrs;
- final String activeChr;
- final double height;
- const ChessBox({
- Key? key,
- required this.itemChrs,
- this.activeChr = '',
- required this.height,
- }) : super(key: key);
- @override
- State<ChessBox> createState() => _ChessBoxState();
- }
- class _ChessBoxState extends State<ChessBox> {
- static const allItemChrs = 'kabcnrp';
- int matchCount(String chr) {
- return RegExp(chr).allMatches(widget.itemChrs).length;
- }
- void setActive(String chr) {
- EditFenState parent = context.findAncestorStateOfType<EditFenState>()!;
- parent.setActiveChr(chr);
- }
- void clearAll() {
- EditFenState parent = context.findAncestorStateOfType<EditFenState>()!;
- parent.clearAll();
- }
- @override
- void initState() {
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return SizedBox(
- width: 114,
- height: widget.height,
- child: Flex(
- direction: Axis.vertical,
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Wrap(
- children: allItemChrs
- .split('')
- .map<Widget>(
- (String chr) => ItemWidget(
- chr: chr,
- count: matchCount(chr),
- isActive: widget.activeChr == chr,
- ),
- )
- .toList(),
- ),
- Wrap(
- children: allItemChrs
- .toUpperCase()
- .split('')
- .map<Widget>(
- (String chr) => ItemWidget(
- chr: chr,
- count: matchCount(chr),
- isActive: widget.activeChr == chr,
- ),
- )
- .toList(),
- ),
- Wrap(
- children: [
- ElevatedButton(
- onPressed: () {
- clearAll();
- },
- child: Text(
- context.l10n.clearAll,
- ),
- )
- ],
- )
- ],
- ),
- );
- }
- }
- class ItemWidget extends StatelessWidget {
- final String chr;
- final int count;
- final bool isActive;
- const ItemWidget({
- Key? key,
- required this.chr,
- required this.count,
- this.isActive = false,
- }) : super(key: key);
- @override
- Widget build(BuildContext context) {
- HomePageState wrapper = context.findAncestorStateOfType<HomePageState>()!;
- GameManager manager = wrapper.gamer;
- _ChessBoxState parent = context.findAncestorStateOfType<_ChessBoxState>()!;
- return GestureDetector(
- onTap: () {
- if (count > 0) {
- parent.setActive(chr);
- }
- },
- child: SizedBox(
- width: manager.skin.size * manager.scale,
- height: manager.skin.size * manager.scale,
- child: Stack(
- children: [
- Piece(
- isActive: isActive,
- item: ChessItem(
- chr,
- ),
- ),
- Align(
- alignment: Alignment.topRight,
- child: Container(
- width: 20,
- height: 20,
- decoration: BoxDecoration(
- color: count > 0 ? Colors.red : Colors.grey,
- borderRadius: const BorderRadius.all(Radius.circular(10)),
- ),
- child: Center(
- child: Text(
- count.toString(),
- style: const TextStyle(color: Colors.white),
- ),
- ),
- ),
- )
- ],
- ),
- ),
- );
- }
- }
|