vehicle_apply_page.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import '../../core/utils/date_utils.dart' as du;
  6. import '../../core/utils/responsive.dart';
  7. import '../../shared/widgets/form_section.dart';
  8. import '../../shared/widgets/form_field_row.dart';
  9. import 'vehicle_apply_controller.dart';
  10. class VehicleApplyPage extends ConsumerStatefulWidget {
  11. final String? editId;
  12. const VehicleApplyPage({super.key, this.editId});
  13. @override
  14. ConsumerState<VehicleApplyPage> createState() => _VehicleApplyPageState();
  15. }
  16. class _VehicleApplyPageState extends ConsumerState<VehicleApplyPage> {
  17. static const _vehicleTypes = ['轿车', '商务车', '中巴', '大巴', '货车'];
  18. static const _purposes = ['客户接待', '商务出行', '内部公务', '其他'];
  19. static const _driverTypes = ['自驾', '配驾'];
  20. @override
  21. Widget build(BuildContext context) {
  22. final ctrl = ref.watch(vehicleApplyProvider(widget.editId).notifier);
  23. final state = ref.watch(vehicleApplyProvider(widget.editId));
  24. final r = ResponsiveHelper.of(context);
  25. return Scaffold(
  26. appBar: TDNavBar(
  27. title: widget.editId != null ? '编辑用车' : '用车申请',
  28. titleColor: Colors.white,
  29. backgroundColor: const Color(0xFF00ABF3),
  30. centerTitle: false,
  31. ),
  32. body: Column(
  33. children: [
  34. Expanded(
  35. child: Align(alignment: Alignment.topCenter,
  36. child: ConstrainedBox(
  37. constraints: BoxConstraints(maxWidth: r.formMaxWidth),
  38. child: SingleChildScrollView(
  39. padding: const EdgeInsets.symmetric(vertical: 8),
  40. child: Column(children: [
  41. FormSection(
  42. title: '基本信息',
  43. children: [
  44. FormFieldRow(label: '车辆类型', value: state.vehicle.vehicleType, onTap: () => _showPicker(_vehicleTypes, ctrl.updateVehicleType, title: '选择车辆类型')),
  45. FormFieldRow(label: '用车目的', value: state.vehicle.purpose, hint: '请选择', onTap: () => _showPicker(_purposes, ctrl.updatePurpose, title: '选择用车目的')),
  46. FormFieldRow(label: '出发地', value: state.vehicle.origin, hint: '请输入', onTap: () => _showInput('出发地', ctrl.updateOrigin)),
  47. FormFieldRow(label: '目的地', value: state.vehicle.destination, hint: '请输入', onTap: () => _showInput('目的地', ctrl.updateDestination)),
  48. FormFieldRow(label: '乘车人数', value: '${state.vehicle.passengerCount}人', onTap: () => _showNumberInput('乘车人数', ctrl.updatePassengerCount, state.vehicle.passengerCount)),
  49. FormFieldRow(label: '驾驶员', value: state.vehicle.driver, hint: '请选择', onTap: () => _showPicker(_driverTypes, ctrl.updateDriver, title: '选择驾驶员')),
  50. FormFieldRow(label: '预估里程', value: state.vehicle.estimatedMileage > 0 ? '${state.vehicle.estimatedMileage}公里' : null, hint: '请输入', onTap: () => _showMileageInput('预估里程', ctrl.updateEstimatedMileage, state.vehicle.estimatedMileage)),
  51. FormFieldRow(label: '用车事由', value: state.vehicle.reason, hint: '请输入事由', onTap: () => _showInput('用车事由', ctrl.updateReason)),
  52. ],
  53. ),
  54. FormSection(
  55. title: '时间',
  56. children: [
  57. FormFieldRow(label: '预计开始时间', value: du.DateUtils.formatDateTime(state.vehicle.startTime), onTap: () => _pickDateTime(ctrl.updateStartTime, state.vehicle.startTime)),
  58. FormFieldRow(label: '预计结束时间', value: du.DateUtils.formatDateTime(state.vehicle.endTime), onTap: () => _pickDateTime(ctrl.updateEndTime, state.vehicle.endTime)),
  59. ],
  60. ),
  61. ]),
  62. ),
  63. ),
  64. ),
  65. ),
  66. _buildBottomButtons(ctrl, state),
  67. ],
  68. ),
  69. );
  70. }
  71. void _showInput(String title, void Function(String) onSave) {
  72. final ctrl = TextEditingController();
  73. showDialog(
  74. context: context,
  75. builder: (_) => TDAlertDialog(
  76. title: title,
  77. contentWidget: TextField(controller: ctrl, decoration: InputDecoration(hintText: '请输入$title', border: const OutlineInputBorder())),
  78. leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)),
  79. rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(ctrl.text); Navigator.pop(context); }),
  80. ),
  81. );
  82. }
  83. void _showNumberInput(String title, void Function(int) onSave, int current) {
  84. final ctrl = TextEditingController(text: '$current');
  85. showDialog(
  86. context: context,
  87. builder: (_) => TDAlertDialog(
  88. title: title,
  89. contentWidget: TextField(controller: ctrl, keyboardType: TextInputType.number, decoration: const InputDecoration(border: OutlineInputBorder())),
  90. leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)),
  91. rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(int.tryParse(ctrl.text) ?? 1); Navigator.pop(context); }),
  92. ),
  93. );
  94. }
  95. void _showMileageInput(String title, void Function(double) onSave, double current) {
  96. final ctrl = TextEditingController(text: current > 0 ? '$current' : '');
  97. showDialog(
  98. context: context,
  99. builder: (_) => TDAlertDialog(
  100. title: title,
  101. contentWidget: TextField(controller: ctrl, keyboardType: TextInputType.number, decoration: const InputDecoration(border: OutlineInputBorder())),
  102. leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)),
  103. rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(double.tryParse(ctrl.text) ?? 0); Navigator.pop(context); }),
  104. ),
  105. );
  106. }
  107. void _showPicker(List<String> options, void Function(String) onPick, {String title = '请选择'}) {
  108. TDPicker.showMultiPicker(
  109. context,
  110. title: title,
  111. data: [options],
  112. onConfirm: (selected) {
  113. onPick(selected.first);
  114. },
  115. );
  116. }
  117. void _pickDateTime(void Function(DateTime) onPicked, DateTime initial) {
  118. TDPicker.showDatePicker(
  119. context,
  120. title: '选择日期时间',
  121. useYear: true,
  122. useMonth: true,
  123. useDay: true,
  124. useHour: true,
  125. useMinute: true,
  126. initialDate: [initial.year, initial.month, initial.day, initial.hour, initial.minute],
  127. onConfirm: (selected) {
  128. onPicked(DateTime(selected['year']!, selected['month']!, selected['day']!, selected['hour']!, selected['minute']!));
  129. },
  130. );
  131. }
  132. Widget _buildBottomButtons(VehicleApplyController ctrl, VehicleApplyState state) {
  133. return Container(
  134. padding: const EdgeInsets.all(12),
  135. decoration: BoxDecoration(color: Colors.white, boxShadow: [BoxShadow(color: Colors.black.withValues(alpha: 0.05), blurRadius: 4, offset: const Offset(0, -1))]),
  136. child: Row(
  137. children: [
  138. Expanded(child: TDButton(
  139. text: '存草稿',
  140. type: TDButtonType.outline,
  141. isBlock: true,
  142. disabled: state.isSubmitting,
  143. onTap: state.isSubmitting ? null : () async { await ctrl.saveDraft(); if (context.mounted) context.pop(); },
  144. )),
  145. const SizedBox(width: 12),
  146. Expanded(flex: 2, child: TDButton(
  147. text: '提交审批',
  148. type: TDButtonType.fill,
  149. theme: TDButtonTheme.primary,
  150. isBlock: true,
  151. disabled: state.isSubmitting,
  152. onTap: state.isSubmitting ? null : () async { final ok = await ctrl.submit(); if (context.mounted && ok) context.pop(); },
  153. )),
  154. ],
  155. ),
  156. );
  157. }
  158. }