play_player.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import 'package:flutter/material.dart';
  2. import '../global.dart';
  3. import '../driver/player_driver.dart';
  4. import '../models/game_event.dart';
  5. import '../widgets/game_wrapper.dart';
  6. import '../models/game_manager.dart';
  7. import '../widgets/tab_card.dart';
  8. /// 组合玩家框及对局双方信息框
  9. class PlayPlayer extends StatefulWidget {
  10. const PlayPlayer({Key? key}) : super(key: key);
  11. @override
  12. State<PlayPlayer> createState() => PlayPlayerState();
  13. }
  14. class PlayPlayerState extends State<PlayPlayer> {
  15. late GameManager gamer = GameManager.instance;
  16. int currentTeam = 0;
  17. @override
  18. void initState() {
  19. super.initState();
  20. GameWrapperState gameWrapper =
  21. context.findAncestorStateOfType<GameWrapperState>()!;
  22. gamer = gameWrapper.gamer;
  23. gamer.on<GamePlayerEvent>(onChangePlayer);
  24. gamer.on<GameLoadEvent>(onReloadGame);
  25. gamer.on<GameResultEvent>(onResult);
  26. }
  27. @override
  28. void dispose() {
  29. gamer.off<GamePlayerEvent>(onChangePlayer);
  30. gamer.off<GameLoadEvent>(onReloadGame);
  31. gamer.off<GameResultEvent>(onResult);
  32. super.dispose();
  33. }
  34. void onResult(GameEvent event) {
  35. setState(() {});
  36. }
  37. void onReloadGame(GameEvent event) {
  38. if (event.data != 0) return;
  39. setState(() {});
  40. }
  41. void onChangePlayer(GameEvent event) {
  42. setState(() {
  43. currentTeam = event.data;
  44. });
  45. }
  46. Widget switchRobot(int team) {
  47. if (gamer.hands[team].isUser) {
  48. return IconButton(
  49. icon: const Icon(Icons.android),
  50. tooltip: context.l10n.trusteeshipToRobots,
  51. onPressed: () {
  52. changePlayDriver(team, DriverType.robot);
  53. },
  54. );
  55. } else if (gamer.hands[team].isRobot) {
  56. return IconButton(
  57. icon: const Icon(
  58. Icons.android,
  59. color: Colors.blueAccent,
  60. ),
  61. tooltip: context.l10n.cancelRobots,
  62. onPressed: () {
  63. changePlayDriver(team, DriverType.user);
  64. },
  65. );
  66. }
  67. return const SizedBox();
  68. }
  69. void changePlayDriver(int team, DriverType driverType) {
  70. setState(() {
  71. gamer.switchDriver(team, driverType);
  72. });
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. BoxDecoration decoration = BoxDecoration(
  77. border: Border.all(color: Colors.grey, width: 0.5),
  78. borderRadius: const BorderRadius.all(Radius.circular(2)),
  79. );
  80. return SizedBox(
  81. width: 229,
  82. child: Column(
  83. children: [
  84. ListTile(
  85. leading: Icon(
  86. Icons.person,
  87. color: currentTeam == 1 ? Colors.blueAccent : Colors.black12,
  88. ),
  89. title: Text(gamer.getPlayer(1).title),
  90. subtitle: Text(currentTeam == 1 ? context.l10n.thinking : ''),
  91. trailing: switchRobot(1),
  92. ),
  93. const SizedBox(width: 10),
  94. ListTile(
  95. leading: Icon(
  96. Icons.person,
  97. color: currentTeam == 0 ? Colors.blueAccent : Colors.black12,
  98. ),
  99. title: Text(gamer.getPlayer(0).title),
  100. subtitle: Text(currentTeam == 0 ? context.l10n.thinking : ''),
  101. trailing: switchRobot(0),
  102. ),
  103. const SizedBox(width: 10),
  104. Expanded(
  105. child: Container(
  106. decoration: decoration,
  107. child: TabCard(
  108. titlePadding: const EdgeInsets.only(top: 10, bottom: 10),
  109. titles: [
  110. Text(context.l10n.currentInfo),
  111. Text(context.l10n.manual)
  112. ],
  113. bodies: [
  114. Center(
  115. child: Column(
  116. mainAxisAlignment: MainAxisAlignment.center,
  117. crossAxisAlignment: CrossAxisAlignment.center,
  118. children: [
  119. Text(gamer.manual.event),
  120. Text(
  121. '${gamer.manual.red} (${gamer.manual.chineseResult}) ${gamer.manual.black}',
  122. ),
  123. Text(
  124. gamer.manual.ecco.isEmpty
  125. ? ''
  126. : '${gamer.manual.opening}(${gamer.manual.ecco})',
  127. ),
  128. ],
  129. ),
  130. ),
  131. Container(
  132. padding: const EdgeInsets.all(10),
  133. child: Table(
  134. border: null,
  135. columnWidths: const {
  136. 0: IntrinsicColumnWidth(),
  137. 1: FlexColumnWidth(),
  138. },
  139. defaultVerticalAlignment: TableCellVerticalAlignment.top,
  140. children: [
  141. TableRow(
  142. children: [
  143. Text(context.l10n.theEvent),
  144. Text(gamer.manual.event)
  145. ],
  146. ),
  147. TableRow(
  148. children: [
  149. Text(context.l10n.theSite),
  150. Text(gamer.manual.site)
  151. ],
  152. ),
  153. TableRow(
  154. children: [
  155. Text(context.l10n.theDate),
  156. Text(gamer.manual.date)
  157. ],
  158. ),
  159. TableRow(
  160. children: [
  161. Text(context.l10n.theRound),
  162. Text(gamer.manual.round)
  163. ],
  164. ),
  165. TableRow(
  166. children: [
  167. Text(context.l10n.theRed),
  168. Text(
  169. '${gamer.manual.redTeam}/${gamer.manual.red}',
  170. ),
  171. ],
  172. ),
  173. TableRow(
  174. children: [
  175. Text(context.l10n.theBlack),
  176. Text(
  177. '${gamer.manual.blackTeam}/${gamer.manual.black}',
  178. ),
  179. ],
  180. ),
  181. ],
  182. ),
  183. )
  184. ],
  185. ),
  186. ),
  187. )
  188. ],
  189. ),
  190. );
  191. }
  192. }