piece.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:cchess/cchess.dart';
  2. import 'package:flutter/material.dart';
  3. import '../models/game_manager.dart';
  4. import '../pages/home_page.dart';
  5. /// 棋子
  6. class Piece extends StatelessWidget {
  7. final ChessItem item;
  8. final bool isActive;
  9. final bool isAblePoint;
  10. final bool isHover;
  11. const Piece({
  12. Key? key,
  13. required this.item,
  14. this.isActive = false,
  15. this.isHover = false,
  16. this.isAblePoint = false,
  17. }) : super(key: key);
  18. Widget blankWidget(GameManager gamer) {
  19. double size = gamer.skin.size;
  20. return Container(
  21. width: size,
  22. height: size,
  23. decoration: const BoxDecoration(color: Colors.transparent),
  24. );
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. GameManager gamer =
  29. context.findAncestorStateOfType<HomePageState>()!.gamer;
  30. String team = item.team == 0 ? 'r' : 'b';
  31. return item.isBlank
  32. ? blankWidget(gamer)
  33. : AnimatedContainer(
  34. width: gamer.skin.size * gamer.scale,
  35. height: gamer.skin.size * gamer.scale,
  36. duration: const Duration(milliseconds: 300),
  37. curve: Curves.easeOutQuint,
  38. transform: isHover
  39. ? (Matrix4.translationValues(-4, -4, -4))
  40. : (Matrix4.translationValues(0, 0, 0)),
  41. transformAlignment: Alignment.topCenter,
  42. decoration: (isHover)
  43. ? BoxDecoration(
  44. boxShadow: const [
  45. BoxShadow(
  46. color: Color.fromRGBO(0, 0, 0, .1),
  47. offset: Offset(2, 3),
  48. blurRadius: 1,
  49. spreadRadius: 0,
  50. ),
  51. BoxShadow(
  52. color: Color.fromRGBO(0, 0, 0, .1),
  53. offset: Offset(4, 6),
  54. blurRadius: 2,
  55. spreadRadius: 2,
  56. )
  57. ],
  58. //border: Border.all(color: Color.fromRGBO(255, 255, 255, .7), width: 2),
  59. borderRadius: BorderRadius.all(
  60. Radius.circular(gamer.skin.size / 2),
  61. ),
  62. )
  63. : BoxDecoration(
  64. boxShadow: const [
  65. BoxShadow(
  66. color: Color.fromRGBO(0, 0, 0, .2),
  67. offset: Offset(2, 2),
  68. blurRadius: 1,
  69. spreadRadius: 0,
  70. ),
  71. BoxShadow(
  72. color: Color.fromRGBO(0, 0, 0, .1),
  73. offset: Offset(3, 3),
  74. blurRadius: 1,
  75. spreadRadius: 1,
  76. ),
  77. ],
  78. border: isActive
  79. ? Border.all(
  80. color: Colors.white54,
  81. width: 2,
  82. style: BorderStyle.solid,
  83. )
  84. : null,
  85. borderRadius: BorderRadius.all(
  86. Radius.circular(gamer.skin.size / 2),
  87. ),
  88. ),
  89. child: Stack(
  90. children: [
  91. Image.asset(
  92. team == 'r'
  93. ? gamer.skin.getRedChess(item.code)
  94. : gamer.skin.getBlackChess(item.code),
  95. ),
  96. ],
  97. ),
  98. );
  99. }
  100. }