field_control.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import 'package:app_business/service/api.dart';
  2. import 'package:grpc/grpc.dart';
  3. import 'package:track_common/service/map_watch.dart';
  4. import 'package:track_common/track_common.dart';
  5. import 'package:track_common/view/home/field_control/field_control.dart';
  6. import 'package:track_common/view/home/field_control/field_control_controller.dart';
  7. import 'package:track_common/widget/prelude.dart';
  8. import 'dialog_event_register.dart';
  9. class FieldControlPageImpl extends FieldControlPage {
  10. const FieldControlPageImpl({super.key});
  11. @override
  12. Widget rightColumn(BuildContext context) {
  13. return Obx(() {
  14. final mapWatch = controller.mapWatch;
  15. debug('${identityHashCode(controller)}');
  16. return Container(
  17. width: 370,
  18. padding: const EdgeInsets.all(6.4),
  19. height: double.infinity,
  20. color: Colors.white,
  21. child: Column(
  22. children: [
  23. Container(
  24. padding: const EdgeInsets.only(bottom: 8),
  25. width: double.infinity,
  26. child: DarkButton(
  27. onPressed: mapWatch != null
  28. ? () => _onTapRegister(context, mapWatch)
  29. : null,
  30. child: const Text('注册比赛'))),
  31. const Expanded(child: EventInfoView())
  32. ],
  33. ));
  34. });
  35. }
  36. Future<void> _onTapRegister(BuildContext context, MapWatch mapWatch) async {
  37. // final r = await Get.dialog(const RegisterDialog(), arguments: mapWatch.id)
  38. // as RegisterInfo?;
  39. final mapId = mapWatch.id;
  40. final rl = await Get.find<ApiService>()
  41. .stub
  42. .toActivitySelectList(IdRequest()..id = Int64(mapId));
  43. final l = rl.list.map((e) => EventInfo()
  44. ..id = e.actId
  45. ..name = e.actName);
  46. final r = await showEventRegisterDialog(mapId, l);
  47. if (r != null) {
  48. Get.find<ApiService>()
  49. .stub
  50. .toMatchRegusterAdd(ToMatchRegusterAddRequest()
  51. ..actId = r.id
  52. ..regName = r.name
  53. ..startAt = r.eventStartAt.toPb()
  54. ..stopAt = r.eventStopAt.toPb()
  55. ..bShowTime = r.showStartAt.toPb()
  56. ..eShowTime = r.showStopAt.toPb()
  57. ..isQueryPwd = r.passwordQuery != null
  58. ..queryPasswd = r.passwordQuery ?? ''
  59. ..matchPasswd = r.passwordEvent ?? '')
  60. .then((p0) {}, onError: (e) {
  61. if (e is GrpcError) {
  62. if (context.mounted) {
  63. ScaffoldMessenger.of(context)
  64. .showSnackBar(SnackBar(content: Text('注册失败:{${e.message}')));
  65. }
  66. }
  67. });
  68. }
  69. }
  70. }
  71. class EventInfo {
  72. int id = 0;
  73. String name = '';
  74. }
  75. class RegisterInfo {
  76. var id = 0;
  77. var name = '';
  78. var startAt = DateTime.now();
  79. var stopAt = DateTime.now();
  80. String? password;
  81. String? passwordQuery;
  82. }
  83. class FieldControlControllerImpl extends FieldControlController {
  84. @override
  85. void onInit() {
  86. debug('init');
  87. super.onInit();
  88. }
  89. @override
  90. void onClose() {
  91. super.onClose();
  92. debug('close');
  93. }
  94. }
  95. class RegisterDialogController extends GetxController {
  96. var registerName = '';
  97. final date = Rx<DateTime?>(null);
  98. final registerStartAt = Rx<TimeOfDay?>(null);
  99. final registerStopAt = Rx<TimeOfDay?>(null);
  100. final selected = Rx<EventInfo?>(null);
  101. final eventList = <EventInfo>[].obs;
  102. final hasPassword = false.obs;
  103. final hasPasswordQuery = false.obs;
  104. var password = '';
  105. var passwordQuery = '';
  106. late final int mapId;
  107. final api = Get.find<ApiService>();
  108. String? get dateString {
  109. final d = date.value;
  110. if (d != null) {
  111. return '${d.month}/${d.day}';
  112. }
  113. return null;
  114. }
  115. @override
  116. void onInit() {
  117. mapId = Get.arguments as int;
  118. api.stub.toActivitySelectList(IdRequest()..id = Int64(mapId)).then((r) {
  119. eventList.value = r.list
  120. .map((e) => EventInfo()
  121. ..id = e.actId
  122. ..name = e.actName)
  123. .toList();
  124. });
  125. super.onInit();
  126. }
  127. }
  128. class RegisterDialog extends GetView<RegisterDialogController> {
  129. const RegisterDialog({super.key});
  130. @override
  131. Widget build(BuildContext context) {
  132. return GetBuilder(
  133. init: RegisterDialogController(),
  134. builder: (c) {
  135. return AlertDialog(
  136. title: const Center(
  137. child: Text(
  138. '注册比赛',
  139. style: TextStyle(fontSize: 17),
  140. )),
  141. backgroundColor: Colors.white,
  142. shape: RoundedRectangleBorder(
  143. borderRadius: BorderRadius.circular(17.78)),
  144. content: SizedBox(
  145. width: 320,
  146. child: ListView(
  147. shrinkWrap: true,
  148. children: [
  149. Obx(() => SizedBox(
  150. child: DropdownMenu<EventInfo>(
  151. key: GlobalKey(),
  152. width: 320,
  153. hintText: '请选择活动',
  154. onSelected: (one) {
  155. controller.selected.value = one;
  156. },
  157. inputDecorationTheme: InputDecorationTheme(
  158. border: textBorder,
  159. isDense: true,
  160. ),
  161. dropdownMenuEntries: controller.eventList
  162. .map((e) => DropdownMenuEntry<EventInfo>(
  163. value: e, label: e.name))
  164. .toList()))),
  165. const SizedBox(height: 21.34),
  166. _TextField(
  167. hint: '请输入名称',
  168. onChanged: (v) {
  169. c.registerName = v;
  170. }),
  171. const SizedBox(height: 21.34),
  172. Row(children: [
  173. Expanded(
  174. child: Obx(() => _TextField(
  175. hint: '日期',
  176. readOnly: true,
  177. initText: c.dateString,
  178. onTap: () async {
  179. c.date.value = await _showDatePicker(
  180. context, c.date.value);
  181. }))),
  182. const SizedBox(width: 15.64),
  183. Expanded(
  184. child: Obx(() => _TextField(
  185. hint: '开始时间',
  186. readOnly: true,
  187. initText:
  188. c.registerStartAt.value?.format(context),
  189. onTap: () async {
  190. c.registerStartAt.value = await _showTimePicker(
  191. context, c.registerStartAt.value);
  192. }))),
  193. const SizedBox(width: 15.64),
  194. Expanded(
  195. child: Obx(() => _TextField(
  196. hint: '结束时间',
  197. readOnly: true,
  198. initText: c.registerStopAt.value?.format(context),
  199. onTap: () async {
  200. c.registerStopAt.value = await _showTimePicker(
  201. context, c.registerStopAt.value);
  202. }))),
  203. ]),
  204. const SizedBox(height: 21.34),
  205. Row(
  206. mainAxisSize: MainAxisSize.min,
  207. children: [
  208. Obx(() => Switch(
  209. value: c.hasPasswordQuery.value,
  210. onChanged: (v) {
  211. c.hasPasswordQuery.value = v;
  212. })),
  213. const Text('查询密码'),
  214. const SizedBox(width: 12),
  215. Obx(() => Expanded(
  216. child: Visibility(
  217. visible: c.hasPasswordQuery.value,
  218. child: _TextField(
  219. hint: '请输入密码',
  220. onChanged: (v) {
  221. c.passwordQuery = v;
  222. })))),
  223. ],
  224. ),
  225. const SizedBox(height: 6),
  226. Row(
  227. mainAxisSize: MainAxisSize.min,
  228. children: [
  229. Obx(() => Switch(
  230. value: c.hasPassword.value,
  231. onChanged: (v) {
  232. c.hasPassword.value = v;
  233. })),
  234. const Text('赛事密码'),
  235. const SizedBox(width: 12),
  236. Obx(() => Expanded(
  237. child: Visibility(
  238. visible: c.hasPassword.value,
  239. child: _TextField(
  240. hint: '请输入密码',
  241. onChanged: (v) {
  242. c.password = v;
  243. })))),
  244. ],
  245. ),
  246. const SizedBox(height: 21.34),
  247. SizedBox(
  248. width: double.infinity,
  249. child: DarkButton(
  250. onPressed: _onRegister, child: const Text('注 册')))
  251. ],
  252. )),
  253. );
  254. });
  255. }
  256. void _onRegister() {
  257. final date = controller.date.value;
  258. final timeStartAt = controller.registerStartAt.value;
  259. final timeStopAt = controller.registerStopAt.value;
  260. final selected = controller.selected.value;
  261. if (selected == null) {
  262. Get.snackbar('错误', '请选择一个活动');
  263. return;
  264. }
  265. if (controller.registerName.isEmpty) {
  266. Get.snackbar('错误', '输入名称');
  267. return;
  268. }
  269. if (date == null) {
  270. Get.snackbar('错误', '请选择日期');
  271. return;
  272. }
  273. if (timeStartAt == null) {
  274. Get.snackbar('错误', '请选择开始时间');
  275. return;
  276. }
  277. if (timeStopAt == null) {
  278. Get.snackbar('错误', '请选择结束时间');
  279. return;
  280. }
  281. final startAt =
  282. date.copyWith(hour: timeStartAt.hour, minute: timeStartAt.minute);
  283. final stopAt =
  284. date.copyWith(hour: timeStopAt.hour, minute: timeStopAt.minute);
  285. if (startAt.isAfter(stopAt)) {
  286. Get.snackbar('错误', '结束时间应晚于开始时间');
  287. return;
  288. }
  289. Get.back(
  290. result: RegisterInfo()
  291. ..id = selected.id
  292. ..name = controller.registerName
  293. ..startAt = startAt
  294. ..stopAt = stopAt
  295. ..password = controller.hasPassword.value ? controller.password : null
  296. ..passwordQuery = controller.hasPasswordQuery.value
  297. ? controller.passwordQuery
  298. : null);
  299. }
  300. Future<TimeOfDay?> _showTimePicker(
  301. BuildContext context, TimeOfDay? init) async {
  302. final TimeOfDay? time = await showTimePicker(
  303. context: context,
  304. initialTime: init ?? TimeOfDay.now(),
  305. );
  306. return time;
  307. }
  308. Future<DateTime?> _showDatePicker(
  309. BuildContext context, DateTime? init) async {
  310. final DateTime? time = await showDatePicker(
  311. context: context,
  312. initialDate: init ?? DateTime.now(),
  313. firstDate: DateTime.now(),
  314. lastDate: DateTime.now().add(365.days),
  315. );
  316. return time;
  317. }
  318. }
  319. final textBorder = OutlineInputBorder(
  320. borderSide: const BorderSide(width: 0.71, color: Color(0xff818181)),
  321. borderRadius: BorderRadius.circular(2.13),
  322. );
  323. class _TextField extends StatelessWidget {
  324. const _TextField(
  325. {required this.hint,
  326. this.onChanged,
  327. this.readOnly = false,
  328. this.onTap,
  329. this.initText});
  330. final String hint;
  331. final void Function(String)? onChanged;
  332. final bool readOnly;
  333. final void Function()? onTap;
  334. final String? initText;
  335. @override
  336. Widget build(BuildContext context) {
  337. return SizedBox(
  338. child: TextFormField(
  339. key: GlobalKey(),
  340. initialValue: initText,
  341. maxLines: 1,
  342. onChanged: onChanged,
  343. onTap: onTap,
  344. readOnly: readOnly,
  345. decoration: InputDecoration(
  346. hintText: hint,
  347. border: textBorder,
  348. isDense: true,
  349. // contentPadding: const EdgeInsets.all(8.53)
  350. )),
  351. );
  352. }
  353. }