vehicle_list_page.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 'package:easy_refresh/easy_refresh.dart';
  6. import '../../core/theme/app_colors.dart';
  7. import '../shell/nav_bar_config.dart';
  8. import '../../core/utils/date_utils.dart' as du;
  9. import '../../shared/widgets/empty_state.dart';
  10. import '../../shared/widgets/loading_widget.dart';
  11. import '../../core/i18n/app_localizations.dart';
  12. import 'vehicle_list_controller.dart';
  13. import 'vehicle_model.dart';
  14. class VehicleListPage extends ConsumerStatefulWidget {
  15. const VehicleListPage({super.key});
  16. @override
  17. ConsumerState<VehicleListPage> createState() => _VehicleListPageState();
  18. }
  19. class _VehicleListPageState extends ConsumerState<VehicleListPage>
  20. with TickerProviderStateMixin {
  21. static const _tabLabels = ['全部', '草稿', '审批中', '已通过', '已拒绝', '已还车'];
  22. static const _tabKeys = [
  23. '',
  24. 'draft',
  25. 'pending',
  26. 'approved',
  27. 'rejected',
  28. 'returned',
  29. ];
  30. late final TabController _tabCtrl;
  31. @override
  32. void initState() {
  33. super.initState();
  34. _tabCtrl = TabController(length: _tabLabels.length, vsync: this);
  35. _tabCtrl.addListener(() {
  36. if (!_tabCtrl.indexIsChanging) {
  37. ref.read(vehicleStatusFilterProvider.notifier).state =
  38. _tabKeys[_tabCtrl.index];
  39. }
  40. });
  41. }
  42. @override
  43. void dispose() {
  44. _tabCtrl.dispose();
  45. super.dispose();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. final status = ref.watch(vehicleStatusFilterProvider);
  50. final l10n = AppLocalizations.of(context);
  51. // Sync TabController with external filter changes
  52. final targetIdx = _tabKeys.indexOf(status);
  53. if (targetIdx >= 0 &&
  54. _tabCtrl.index != targetIdx &&
  55. !_tabCtrl.indexIsChanging) {
  56. WidgetsBinding.instance.addPostFrameCallback((_) {
  57. if (mounted) _tabCtrl.animateTo(targetIdx);
  58. });
  59. }
  60. ref
  61. .read(navBarConfigProvider.notifier)
  62. .update(
  63. NavBarConfig(
  64. title: l10n.get('vehicleList'),
  65. showBack: true,
  66. onBack: () => context.pop(),
  67. ),
  68. );
  69. return Column(
  70. children: [
  71. Container(
  72. color: AppColors.bgCard,
  73. padding: const EdgeInsets.symmetric(horizontal: 8),
  74. child: TDTabBar(
  75. tabs: _tabLabels.map((l) => TDTab(text: l)).toList(),
  76. controller: _tabCtrl,
  77. isScrollable: true,
  78. labelColor: AppColors.primary,
  79. unselectedLabelColor: AppColors.textSecondary,
  80. outlineType: TDTabBarOutlineType.filled,
  81. showIndicator: true,
  82. indicatorColor: AppColors.primary,
  83. indicatorHeight: 3,
  84. labelPadding: const EdgeInsets.symmetric(horizontal: 12),
  85. onTap: (index) {
  86. ref.read(vehicleStatusFilterProvider.notifier).state =
  87. _tabKeys[index];
  88. },
  89. ),
  90. ),
  91. Expanded(
  92. child: TabBarView(
  93. controller: _tabCtrl,
  94. children: List.generate(_tabKeys.length, (tabIdx) {
  95. return _buildTabContent(tabIdx);
  96. }),
  97. ),
  98. ),
  99. ],
  100. );
  101. }
  102. Widget _buildTabContent(int tabIdx) {
  103. return _VehicleTabContent(statusKey: _tabKeys[tabIdx]);
  104. }
  105. }
  106. class _VehicleTabContent extends ConsumerWidget {
  107. final String statusKey;
  108. const _VehicleTabContent({required this.statusKey});
  109. @override
  110. Widget build(BuildContext context, WidgetRef ref) {
  111. final itemsAsync = ref.watch(vehicleListProvider);
  112. return itemsAsync.when(
  113. loading: () => const LoadingWidget(),
  114. error: (_, _) => const EmptyState(message: '加载失败'),
  115. data: (items) {
  116. if (items.isEmpty) {
  117. return EasyRefresh(
  118. header: TDRefreshHeader(),
  119. onRefresh: () async {
  120. ref.invalidate(vehicleListProvider);
  121. },
  122. child: ListView(
  123. children: const [
  124. SizedBox(height: 120),
  125. EmptyState(message: '暂无用车记录'),
  126. ],
  127. ),
  128. );
  129. }
  130. return EasyRefresh(
  131. header: TDRefreshHeader(),
  132. onRefresh: () async {
  133. ref.invalidate(vehicleListProvider);
  134. },
  135. child: ListView.builder(
  136. padding: const EdgeInsets.all(16),
  137. itemCount: items.length,
  138. itemBuilder: (_, i) => Padding(
  139. padding: const EdgeInsets.only(bottom: 16),
  140. child: _buildVehicleListItem(context, items[i]),
  141. ),
  142. ),
  143. );
  144. },
  145. );
  146. }
  147. Widget _buildVehicleListItem(BuildContext context, VehicleModel item) {
  148. Color bg, fg;
  149. String label;
  150. switch (item.status) {
  151. case 'pending':
  152. bg = AppColors.warningBg;
  153. fg = AppColors.warning;
  154. label = '审批中';
  155. case 'approved':
  156. bg = AppColors.successBg;
  157. fg = AppColors.success;
  158. label = '已通过';
  159. case 'rejected':
  160. bg = AppColors.dangerBg;
  161. fg = AppColors.danger;
  162. label = '已拒绝';
  163. case 'draft':
  164. bg = AppColors.bgPage;
  165. fg = AppColors.statusGray;
  166. label = '草稿';
  167. case 'revoked':
  168. bg = AppColors.revokedBg;
  169. fg = AppColors.revokedText;
  170. label = '已撤回';
  171. case 'returned':
  172. bg = const Color(0xFFEDF2FC);
  173. fg = const Color(0xFF5A8CDB);
  174. label = '已还车';
  175. default:
  176. bg = AppColors.bgPage;
  177. fg = AppColors.statusGray;
  178. label = item.status;
  179. }
  180. return GestureDetector(
  181. onTap: () => context.push('/vehicle/detail/${item.id}'),
  182. child: Container(
  183. padding: const EdgeInsets.all(12),
  184. decoration: BoxDecoration(
  185. color: AppColors.bgCard,
  186. borderRadius: BorderRadius.circular(8),
  187. ),
  188. child: Column(
  189. children: [
  190. // R1: 车牌号 + 状态标签
  191. Row(
  192. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  193. children: [
  194. Text(
  195. item.licensePlate.isNotEmpty ? item.licensePlate : '未指定车辆',
  196. style: const TextStyle(
  197. fontSize: AppFontSizes.subtitle,
  198. fontWeight: FontWeight.w700,
  199. color: AppColors.textPrimary,
  200. ),
  201. ),
  202. Container(
  203. padding: const EdgeInsets.symmetric(
  204. horizontal: 8,
  205. vertical: 2,
  206. ),
  207. decoration: BoxDecoration(
  208. color: bg,
  209. borderRadius: BorderRadius.circular(4),
  210. ),
  211. child: Text(
  212. label,
  213. style: TextStyle(
  214. fontSize: AppFontSizes.caption,
  215. fontWeight: FontWeight.w500,
  216. color: fg,
  217. ),
  218. ),
  219. ),
  220. ],
  221. ),
  222. const SizedBox(height: 6),
  223. // R2: 申请单号 + 用途标签
  224. Row(
  225. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  226. children: [
  227. Text(
  228. item.applicationNo,
  229. style: const TextStyle(
  230. fontSize: AppFontSizes.caption,
  231. color: AppColors.textSecondary,
  232. ),
  233. ),
  234. Container(
  235. padding: const EdgeInsets.symmetric(
  236. horizontal: 6,
  237. vertical: 1,
  238. ),
  239. decoration: BoxDecoration(
  240. color: AppColors.primaryLight,
  241. borderRadius: BorderRadius.circular(3),
  242. ),
  243. child: Text(
  244. item.purpose.isNotEmpty ? item.purpose : '公务',
  245. style: const TextStyle(
  246. fontSize: 10,
  247. color: AppColors.primary,
  248. ),
  249. ),
  250. ),
  251. ],
  252. ),
  253. const SizedBox(height: 6),
  254. // R3: 路线 + 时间
  255. Row(
  256. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  257. children: [
  258. Flexible(
  259. child: Text(
  260. '${item.origin.isNotEmpty ? item.origin : '未知'} → ${item.destination.isNotEmpty ? item.destination : '未知'}',
  261. style: const TextStyle(
  262. fontSize: 13,
  263. color: AppColors.textSecondary,
  264. ),
  265. overflow: TextOverflow.ellipsis,
  266. ),
  267. ),
  268. const SizedBox(width: 8),
  269. Text(
  270. '${du.DateUtils.formatMonthDay(item.startTime)} ${du.DateUtils.formatTime(item.startTime)}',
  271. style: const TextStyle(
  272. fontSize: AppFontSizes.caption,
  273. color: AppColors.textPlaceholder,
  274. ),
  275. ),
  276. ],
  277. ),
  278. ],
  279. ),
  280. ),
  281. );
  282. }
  283. }