play_single_player.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'package:flutter/material.dart';
  2. import '../global.dart';
  3. import '../driver/player_driver.dart';
  4. import '../models/game_event.dart';
  5. import '../models/game_manager.dart';
  6. import '../widgets/list_item.dart';
  7. /// 单个玩家框
  8. class PlaySinglePlayer extends StatefulWidget {
  9. final int team;
  10. final Alignment placeAt;
  11. const PlaySinglePlayer({
  12. Key? key,
  13. required this.team,
  14. this.placeAt = Alignment.topCenter,
  15. }) : super(key: key);
  16. @override
  17. State<PlaySinglePlayer> createState() => PlaySinglePlayerState();
  18. }
  19. class PlaySinglePlayerState extends State<PlaySinglePlayer> {
  20. late GameManager gamer = GameManager.instance;
  21. int currentTeam = 0;
  22. @override
  23. void initState() {
  24. super.initState();
  25. gamer.on<GamePlayerEvent>(onChangePlayer);
  26. gamer.on<GameLoadEvent>(onReloadGame);
  27. gamer.on<GameResultEvent>(onResult);
  28. }
  29. @override
  30. void dispose() {
  31. gamer.off<GamePlayerEvent>(onChangePlayer);
  32. gamer.off<GameLoadEvent>(onReloadGame);
  33. gamer.off<GameResultEvent>(onResult);
  34. super.dispose();
  35. }
  36. void onResult(GameEvent event) {
  37. setState(() {});
  38. }
  39. void onReloadGame(GameEvent event) {
  40. if (event.data != 0) return;
  41. setState(() {});
  42. }
  43. void onChangePlayer(GameEvent event) {
  44. setState(() {
  45. currentTeam = event.data;
  46. });
  47. }
  48. Widget switchRobot(int team) {
  49. if (gamer.hands[team].isUser) {
  50. return IconButton(
  51. icon: const Icon(Icons.android),
  52. tooltip: context.l10n.trusteeshipToRobots,
  53. onPressed: () {
  54. changePlayDriver(team, DriverType.robot);
  55. },
  56. );
  57. } else if (gamer.hands[team].isRobot) {
  58. return IconButton(
  59. icon: const Icon(
  60. Icons.android,
  61. color: Colors.blueAccent,
  62. ),
  63. tooltip: context.l10n.cancelRobots,
  64. onPressed: () {
  65. changePlayDriver(team, DriverType.user);
  66. },
  67. );
  68. }
  69. return const SizedBox();
  70. }
  71. void changePlayDriver(int team, DriverType driverType) {
  72. setState(() {
  73. gamer.switchDriver(team, driverType);
  74. });
  75. }
  76. @override
  77. Widget build(BuildContext context) {
  78. Widget leading;
  79. Widget trailing;
  80. TextDirection tDirect;
  81. if (widget.placeAt == Alignment.topCenter) {
  82. leading = Icon(
  83. Icons.person,
  84. size: 28,
  85. color: currentTeam == widget.team ? Colors.blueAccent : Colors.black12,
  86. );
  87. trailing = switchRobot(widget.team);
  88. tDirect = TextDirection.ltr;
  89. } else {
  90. trailing = Icon(
  91. Icons.person,
  92. size: 28,
  93. color: currentTeam == widget.team ? Colors.blueAccent : Colors.black12,
  94. );
  95. leading = switchRobot(widget.team);
  96. tDirect = TextDirection.rtl;
  97. }
  98. List<Widget> childs = [
  99. SizedBox(
  100. width: 229,
  101. child: ListItem(
  102. leading: leading,
  103. title: Text(
  104. gamer.getPlayer(widget.team).title,
  105. style: const TextStyle(fontSize: 14),
  106. textDirection: tDirect,
  107. ),
  108. subtitle: currentTeam == widget.team
  109. ? Text(
  110. context.l10n.thinking,
  111. style: const TextStyle(fontSize: 10),
  112. textDirection: tDirect,
  113. )
  114. : null,
  115. trailing: trailing,
  116. titleAlign: widget.placeAt == Alignment.topCenter
  117. ? CrossAxisAlignment.start
  118. : CrossAxisAlignment.end,
  119. ),
  120. ),
  121. const SizedBox(width: 10),
  122. ];
  123. return Container(
  124. padding: const EdgeInsets.symmetric(horizontal: 10),
  125. child: Row(
  126. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  127. children: widget.placeAt == Alignment.topCenter
  128. ? childs
  129. : childs.reversed.toList(),
  130. ),
  131. );
  132. }
  133. }