vehicle_list_page.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 '../../core/theme/app_colors_extension.dart';
  8. import '../../core/auth/role_provider.dart';
  9. import '../../shared/widgets/nav_bar_config.dart';
  10. import '../../core/utils/date_utils.dart' as du;
  11. import '../../shared/widgets/empty_state.dart';
  12. import '../../shared/widgets/skeleton_list_card.dart';
  13. import '../../shared/widgets/list_filter_panel.dart';
  14. import '../../shared/widgets/list_footer.dart';
  15. import '../../shared/widgets/status_tag.dart';
  16. import '../../core/i18n/app_localizations.dart';
  17. import 'vehicle_list_controller.dart';
  18. import 'vehicle_model.dart';
  19. final _scopeProvider = StateProvider<String>((ref) => 'my');
  20. class VehicleListPage extends ConsumerStatefulWidget {
  21. const VehicleListPage({super.key});
  22. @override
  23. ConsumerState<VehicleListPage> createState() => _VehicleListPageState();
  24. }
  25. class _VehicleListPageState extends ConsumerState<VehicleListPage>
  26. with TickerProviderStateMixin {
  27. List<String> _getTabLabels(AppLocalizations l10n) => [
  28. l10n.get('all'),
  29. l10n.get('draft'),
  30. l10n.get('pending'),
  31. l10n.get('approved'),
  32. l10n.get('rejected'),
  33. l10n.get('withdrawn'),
  34. l10n.get('returned'),
  35. ];
  36. static const _tabKeys = [
  37. '',
  38. 'draft',
  39. 'pending',
  40. 'approved',
  41. 'rejected',
  42. 'withdrawn',
  43. 'returned',
  44. ];
  45. late final TabController _tabCtrl;
  46. @override
  47. void initState() {
  48. super.initState();
  49. _tabCtrl = TabController(length: _tabKeys.length, vsync: this);
  50. _tabCtrl.addListener(() {
  51. if (!_tabCtrl.indexIsChanging) {
  52. ref.read(vehicleStatusFilterProvider.notifier).state =
  53. _tabKeys[_tabCtrl.index];
  54. }
  55. });
  56. }
  57. @override
  58. void dispose() {
  59. _tabCtrl.dispose();
  60. super.dispose();
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. final status = ref.watch(vehicleStatusFilterProvider);
  65. final dateStart = ref.watch(vehicleDateStartProvider);
  66. final dateEnd = ref.watch(vehicleDateEndProvider);
  67. final purposeFilter = ref.watch(vehiclePurposeFilterProvider);
  68. final l10n = AppLocalizations.of(context);
  69. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  70. final isManager = ref.watch(isManagerProvider);
  71. // Sync TabController with external filter changes
  72. final targetIdx = _tabKeys.indexOf(status);
  73. if (targetIdx >= 0 &&
  74. _tabCtrl.index != targetIdx &&
  75. !_tabCtrl.indexIsChanging) {
  76. WidgetsBinding.instance.addPostFrameCallback((_) {
  77. if (mounted) _tabCtrl.animateTo(targetIdx);
  78. });
  79. }
  80. final filterGroups = [
  81. FilterGroup(
  82. title: l10n.get('filterDateRange'),
  83. type: FilterGroupType.dateRange,
  84. sections: [
  85. FilterSection(
  86. label: l10n.get('filterDateRange'),
  87. type: FilterSectionType.dateRange,
  88. startDate: dateStart,
  89. endDate: dateEnd,
  90. onStartChanged: (v) =>
  91. ref.read(vehicleDateStartProvider.notifier).state = v,
  92. onEndChanged: (v) =>
  93. ref.read(vehicleDateEndProvider.notifier).state = v,
  94. ),
  95. ],
  96. ),
  97. FilterGroup(
  98. title: l10n.get('other'),
  99. type: FilterGroupType.other,
  100. sections: [
  101. FilterSection(
  102. label: l10n.get('vehiclePurpose'),
  103. type: FilterSectionType.singleSelect,
  104. options: [
  105. FilterOption(
  106. value: 'reception',
  107. label: l10n.get('customerReception'),
  108. ),
  109. FilterOption(value: 'business', label: l10n.get('businessTrip')),
  110. FilterOption(value: 'official', label: l10n.get('official')),
  111. ],
  112. selectedValue: purposeFilter,
  113. onChanged: (v) =>
  114. ref.read(vehiclePurposeFilterProvider.notifier).state = v,
  115. ),
  116. ],
  117. ),
  118. ];
  119. final hasFilter = ListFilterPanel.hasActiveFilter(filterGroups);
  120. final filterVersion = Object.hash(dateStart, dateEnd, purposeFilter);
  121. final now = DateTime.now();
  122. void onFilterReset() {
  123. ref.read(vehicleDateStartProvider.notifier).state = null;
  124. ref.read(vehicleDateEndProvider.notifier).state = null;
  125. ref.read(vehiclePurposeFilterProvider.notifier).state = null;
  126. }
  127. setNavBarTitle(context, ref, NavBarConfig(
  128. title: l10n.get('vehicleList'),
  129. showBack: true,
  130. showRight: true,
  131. rightWidget: GestureDetector(
  132. onTap: () => ListFilterPanel.show(
  133. context,
  134. groups: filterGroups,
  135. onReset: onFilterReset,
  136. onConfirm: () {},
  137. defaultStartDate: DateTime(now.year, now.month, 1),
  138. defaultEndDate: DateTime(now.year, now.month, now.day),
  139. ),
  140. child: Stack(
  141. clipBehavior: Clip.none,
  142. children: [
  143. Icon(
  144. TDIcons.filter,
  145. size: 22,
  146. color: hasFilter ? colors.primary : colors.textPrimary,
  147. ),
  148. if (hasFilter)
  149. Positioned(
  150. right: -2,
  151. top: -2,
  152. child: Container(
  153. width: 6,
  154. height: 6,
  155. decoration: BoxDecoration(
  156. color: colors.danger,
  157. shape: BoxShape.circle,
  158. ),
  159. ),
  160. ),
  161. ],
  162. ),
  163. ),
  164. hasFilter: hasFilter,
  165. filterVersion: filterVersion,
  166. onBack: () => context.pop(),
  167. ));
  168. return Column(
  169. children: [
  170. if (isManager)
  171. Container(
  172. color: colors.bgCard,
  173. padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
  174. child: _buildScopeChip(colors),
  175. ),
  176. Container(
  177. color: colors.bgCard,
  178. padding: EdgeInsets.zero,
  179. child: TDSearchBar(
  180. placeHolder: l10n.get('searchVehicle'),
  181. needCancel: true,
  182. style: TDSearchStyle.round,
  183. ),
  184. ),
  185. Container(
  186. color: colors.bgCard,
  187. padding: const EdgeInsets.symmetric(horizontal: 8),
  188. child: TDTabBar(
  189. tabs: _getTabLabels(l10n).map((l) => TDTab(text: l)).toList(),
  190. controller: _tabCtrl,
  191. isScrollable: true,
  192. labelColor: colors.primary,
  193. unselectedLabelColor: colors.textSecondary,
  194. outlineType: TDTabBarOutlineType.filled,
  195. showIndicator: true,
  196. indicatorColor: colors.primary,
  197. indicatorHeight: 3,
  198. dividerHeight: 0,
  199. labelPadding: const EdgeInsets.symmetric(horizontal: 12),
  200. onTap: (index) {
  201. ref.read(vehicleStatusFilterProvider.notifier).state =
  202. _tabKeys[index];
  203. },
  204. ),
  205. ),
  206. Expanded(
  207. child: Container(
  208. color: colors.bgPage,
  209. child: TabBarView(
  210. controller: _tabCtrl,
  211. children: List.generate(_tabKeys.length, (tabIdx) {
  212. return _buildTabContent(tabIdx);
  213. }),
  214. ),
  215. ),
  216. ),
  217. ],
  218. );
  219. }
  220. Widget _buildScopeChip(AppColorsExtension colors) {
  221. final scope = ref.watch(_scopeProvider);
  222. final l10n = AppLocalizations.of(context);
  223. return Row(
  224. children: [
  225. GestureDetector(
  226. onTap: () => ref.read(_scopeProvider.notifier).state = 'my',
  227. child: Container(
  228. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
  229. decoration: BoxDecoration(
  230. color: scope == 'my' ? colors.primary : colors.bgPage,
  231. borderRadius: BorderRadius.circular(16),
  232. border: scope == 'my' ? null : Border.all(color: colors.border),
  233. ),
  234. child: Text(
  235. l10n.get('scopeMyApplications'),
  236. style: TextStyle(
  237. fontSize: 13,
  238. color: scope == 'my' ? colors.bgCard : colors.textSecondary,
  239. ),
  240. ),
  241. ),
  242. ),
  243. const SizedBox(width: 8),
  244. GestureDetector(
  245. onTap: () => ref.read(_scopeProvider.notifier).state = 'sub',
  246. child: Container(
  247. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
  248. decoration: BoxDecoration(
  249. color: scope == 'sub' ? colors.primary : colors.bgPage,
  250. borderRadius: BorderRadius.circular(16),
  251. border: scope == 'sub' ? null : Border.all(color: colors.border),
  252. ),
  253. child: Text(
  254. l10n.get('scopeSubordinates'),
  255. style: TextStyle(
  256. fontSize: 13,
  257. color: scope == 'sub' ? colors.bgCard : colors.textSecondary,
  258. ),
  259. ),
  260. ),
  261. ),
  262. ],
  263. );
  264. }
  265. Widget _buildTabContent(int tabIdx) {
  266. return _VehicleTabContent(statusKey: _tabKeys[tabIdx]);
  267. }
  268. }
  269. class _VehicleTabContent extends ConsumerWidget {
  270. final String statusKey;
  271. const _VehicleTabContent({required this.statusKey});
  272. @override
  273. Widget build(BuildContext context, WidgetRef ref) {
  274. final itemsAsync = ref.watch(vehicleListProvider(statusKey));
  275. final scope = ref.watch(_scopeProvider);
  276. if (itemsAsync.isLoading && !itemsAsync.hasValue) {
  277. return SkeletonLoadingList(
  278. cardBuilder: () => const SkeletonVehicleCard(),
  279. );
  280. }
  281. return EasyRefresh(
  282. header: TDRefreshHeader(),
  283. onRefresh: () async {
  284. ref.read(vehicleRefreshProvider.notifier).state++;
  285. },
  286. child: _buildContent(itemsAsync, context, ref, scope),
  287. );
  288. }
  289. Widget _buildContent(
  290. AsyncValue<List<VehicleModel>> itemsAsync,
  291. BuildContext context,
  292. WidgetRef ref,
  293. String scope,
  294. ) {
  295. final l10n = AppLocalizations.of(context);
  296. final isSub = scope == 'sub';
  297. if (itemsAsync.isReloading) {
  298. final oldItems = itemsAsync.valueOrNull ?? [];
  299. if (oldItems.isEmpty) {
  300. return ListView(
  301. children: [
  302. const SizedBox(height: 120),
  303. EmptyState(message: l10n.get('noVehicles')),
  304. ],
  305. );
  306. }
  307. return ListView.builder(
  308. padding: const EdgeInsets.all(16),
  309. itemCount: oldItems.length,
  310. itemBuilder: (_, i) {
  311. final card = _buildVehicleListItem(
  312. context,
  313. oldItems[i],
  314. isSub: isSub,
  315. );
  316. if (isSub && oldItems[i].status == 'pending') {
  317. return Padding(
  318. padding: const EdgeInsets.only(bottom: 16),
  319. child: _buildSwipeApprove(card, oldItems[i].id),
  320. );
  321. }
  322. return Padding(
  323. padding: const EdgeInsets.only(bottom: 16),
  324. child: card,
  325. );
  326. },
  327. );
  328. }
  329. if (itemsAsync.hasError) {
  330. return ListView(
  331. children: [
  332. const SizedBox(height: 120),
  333. EmptyState(message: l10n.get('loadFailed')),
  334. ],
  335. );
  336. }
  337. final items = itemsAsync.requireValue;
  338. if (items.isEmpty) {
  339. return ListView(
  340. children: [
  341. const SizedBox(height: 120),
  342. EmptyState(message: l10n.get('noVehicles')),
  343. ],
  344. );
  345. }
  346. return ListView.builder(
  347. padding: const EdgeInsets.all(16),
  348. itemCount: items.length + 1,
  349. itemBuilder: (_, i) {
  350. if (i == items.length) return ListFooter(itemCount: items.length);
  351. final card = _buildVehicleListItem(context, items[i], isSub: isSub);
  352. if (isSub && items[i].status == 'pending') {
  353. return Padding(
  354. padding: const EdgeInsets.only(bottom: 16),
  355. child: _buildSwipeApprove(card, items[i].id),
  356. );
  357. }
  358. return Padding(padding: const EdgeInsets.only(bottom: 16), child: card);
  359. },
  360. );
  361. }
  362. Widget _buildVehicleListItem(
  363. BuildContext context,
  364. VehicleModel item, {
  365. bool isSub = false,
  366. }) {
  367. final l10n = AppLocalizations.of(context);
  368. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  369. return GestureDetector(
  370. onTap: () => context.push('/vehicle/detail/${item.id}'),
  371. child: Container(
  372. padding: const EdgeInsets.all(12),
  373. decoration: BoxDecoration(
  374. color: colors.bgCard,
  375. borderRadius: BorderRadius.circular(8),
  376. ),
  377. child: Column(
  378. children: [
  379. // R1: 车牌号 + 状态标签
  380. Row(
  381. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  382. children: [
  383. Text(
  384. item.licensePlate.isNotEmpty ? item.licensePlate : '未指定车辆',
  385. style: TextStyle(
  386. fontSize: AppFontSizes.subtitle,
  387. fontWeight: FontWeight.w700,
  388. color: colors.textPrimary,
  389. ),
  390. ),
  391. StatusTag.fromStatus(item.status, l10n),
  392. ],
  393. ),
  394. if (isSub) ...[
  395. const SizedBox(height: 4),
  396. Align(
  397. alignment: Alignment.centerLeft,
  398. child: Text(
  399. '申请人: ${item.applicantName} · ${item.deptName}',
  400. style: TextStyle(
  401. fontSize: AppFontSizes.caption,
  402. color: colors.textPlaceholder,
  403. ),
  404. ),
  405. ),
  406. ],
  407. const SizedBox(height: 6),
  408. // R2: 申请单号 + 用途标签
  409. Row(
  410. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  411. children: [
  412. Text(
  413. item.applicationNo,
  414. style: TextStyle(
  415. fontSize: AppFontSizes.caption,
  416. color: colors.textSecondary,
  417. ),
  418. ),
  419. Container(
  420. padding: const EdgeInsets.symmetric(
  421. horizontal: 6,
  422. vertical: 1,
  423. ),
  424. decoration: BoxDecoration(
  425. color: colors.primaryLight,
  426. borderRadius: BorderRadius.circular(3),
  427. ),
  428. child: Text(
  429. item.purpose.isNotEmpty ? item.purpose : '公务',
  430. style: TextStyle(fontSize: 10, color: colors.primary),
  431. ),
  432. ),
  433. ],
  434. ),
  435. const SizedBox(height: 6),
  436. // R3: 路线 + 时间
  437. Row(
  438. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  439. children: [
  440. Flexible(
  441. child: Text(
  442. '${item.origin.isNotEmpty ? item.origin : '未知'} → ${item.destination.isNotEmpty ? item.destination : '未知'}',
  443. style: TextStyle(fontSize: 13, color: colors.textSecondary),
  444. overflow: TextOverflow.ellipsis,
  445. ),
  446. ),
  447. const SizedBox(width: 8),
  448. Text(
  449. '${du.DateUtils.formatMonthDay(item.startTime)} ${du.DateUtils.formatTime(item.startTime)}',
  450. style: TextStyle(
  451. fontSize: AppFontSizes.caption,
  452. color: colors.textPlaceholder,
  453. ),
  454. ),
  455. ],
  456. ),
  457. ],
  458. ),
  459. ),
  460. );
  461. }
  462. Widget _buildSwipeApprove(Widget card, String itemId) {
  463. return Builder(
  464. builder: (ctx) {
  465. final screenWidth = MediaQuery.of(ctx).size.width;
  466. return TDSwipeCell(
  467. groupTag: 'vehicle_approve',
  468. right: TDSwipeCellPanel(
  469. extentRatio: 100 / screenWidth,
  470. children: [
  471. TDSwipeCellAction(
  472. label: '',
  473. backgroundColor: Colors.transparent,
  474. builder: (_) => Container(
  475. margin: const EdgeInsets.symmetric(
  476. horizontal: 4,
  477. vertical: 8,
  478. ),
  479. decoration: BoxDecoration(
  480. color: Colors.green,
  481. borderRadius: BorderRadius.circular(8),
  482. ),
  483. alignment: Alignment.center,
  484. padding: const EdgeInsets.symmetric(horizontal: 12),
  485. child: const Text(
  486. '一键同意',
  487. style: TextStyle(
  488. color: Colors.white,
  489. fontSize: 14,
  490. fontWeight: FontWeight.w600,
  491. ),
  492. ),
  493. ),
  494. onPressed: (_) async {
  495. final confirmed = await showDialog<bool>(
  496. context: ctx,
  497. builder: (dCtx) => TDAlertDialog(
  498. title: '确认审批',
  499. content: '确认同意该用车申请?',
  500. leftBtn: TDDialogButtonOptions(
  501. title: '取消',
  502. action: () => Navigator.of(dCtx).pop(false),
  503. ),
  504. rightBtn: TDDialogButtonOptions(
  505. title: '确认',
  506. action: () => Navigator.of(dCtx).pop(true),
  507. ),
  508. ),
  509. );
  510. if (confirmed == true) {
  511. // TODO: 接入实际审批 API
  512. if (ctx.mounted) {
  513. TDToast.showSuccess('已审批通过', context: ctx);
  514. }
  515. }
  516. },
  517. ),
  518. ],
  519. ),
  520. cell: card,
  521. );
  522. },
  523. );
  524. }
  525. }