overtime_list_page.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/theme/app_colors.dart';
  6. import '../../core/utils/date_utils.dart' as du;
  7. import '../../core/utils/responsive.dart';
  8. import '../../shared/widgets/app_card.dart';
  9. import '../../shared/widgets/status_tag.dart';
  10. import '../../shared/widgets/empty_state.dart';
  11. import '../../shared/widgets/loading_widget.dart';
  12. import 'overtime_list_controller.dart';
  13. import 'overtime_model.dart';
  14. class OvertimeListPage extends ConsumerStatefulWidget {
  15. const OvertimeListPage({super.key});
  16. @override
  17. ConsumerState<OvertimeListPage> createState() => _OvertimeListPageState();
  18. }
  19. class _OvertimeListPageState extends ConsumerState<OvertimeListPage> {
  20. final _scrollCtrl = ScrollController();
  21. @override
  22. void initState() {
  23. super.initState();
  24. _scrollCtrl.addListener(_onScroll);
  25. }
  26. void _onScroll() {
  27. if (_scrollCtrl.position.pixels >= _scrollCtrl.position.maxScrollExtent - 100) {
  28. ref.read(overtimePageProvider.notifier).state++;
  29. }
  30. }
  31. @override
  32. void dispose() {
  33. _scrollCtrl.removeListener(_onScroll);
  34. _scrollCtrl.dispose();
  35. super.dispose();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. final status = ref.watch(overtimeStatusFilterProvider);
  40. final itemsAsync = ref.watch(overtimeListProvider);
  41. final r = ResponsiveHelper.of(context);
  42. return Scaffold(
  43. appBar: TDNavBar(
  44. title: '加班申请',
  45. titleColor: Colors.white,
  46. backgroundColor: const Color(0xFF00ABF3),
  47. centerTitle: false,
  48. rightBarItems: [TDNavBarItem(icon: Icons.add, iconColor: Colors.white, action: () => context.push('/overtime/apply'))],
  49. ),
  50. body: Column(children: [
  51. _buildStatusFilter(status),
  52. Expanded(
  53. child: Center(
  54. child: ConstrainedBox(
  55. constraints: BoxConstraints(maxWidth: r.listMaxWidth),
  56. child: itemsAsync.when(
  57. loading: () => const LoadingWidget(),
  58. error: (_, __) => const EmptyState(message: '加载失败'),
  59. data: (items) => items.isEmpty
  60. ? const EmptyState(message: '暂无加班申请')
  61. : RefreshIndicator(
  62. onRefresh: () async { ref.invalidate(overtimeListProvider); },
  63. child: ListView.builder(
  64. controller: _scrollCtrl,
  65. padding: const EdgeInsets.symmetric(vertical: 4),
  66. itemCount: items.length,
  67. itemBuilder: (_, i) => _buildItem(items[i]),
  68. ),
  69. ),
  70. ),
  71. ),
  72. ),
  73. ),
  74. ]),
  75. );
  76. }
  77. Widget _buildStatusFilter(String current) {
  78. final statuses = [
  79. {'key': '', 'label': '全部'},
  80. {'key': 'pending', 'label': '待审批'},
  81. {'key': 'approved', 'label': '已通过'},
  82. {'key': 'rejected', 'label': '已拒绝'},
  83. ];
  84. return Padding(
  85. padding: const EdgeInsets.only(top: 12, bottom: 4, left: 12),
  86. child: SingleChildScrollView(
  87. scrollDirection: Axis.horizontal,
  88. child: Row(
  89. children: statuses.map((s) {
  90. final selected = current == s['key'];
  91. return Padding(
  92. padding: const EdgeInsets.only(right: 8),
  93. child: GestureDetector(
  94. onTap: () {
  95. ref.read(overtimeStatusFilterProvider.notifier).state = s['key']!;
  96. },
  97. child: TDTag(
  98. s['label']!,
  99. size: TDTagSize.small,
  100. theme: selected ? TDTagTheme.primary : null,
  101. isLight: !selected,
  102. shape: TDTagShape.round,
  103. ),
  104. ),
  105. );
  106. }).toList(),
  107. ),
  108. ),
  109. );
  110. }
  111. Widget _buildItem(OvertimeModel item) {
  112. return AppCard(
  113. margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
  114. onTap: () => context.push('/overtime/detail/${item.id}'),
  115. child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
  116. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  117. Text(item.applicationNo, style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14, color: AppColors.textPrimary)),
  118. StatusTag(status: item.status),
  119. ]),
  120. const SizedBox(height: 4),
  121. Text('${item.otType} · ${item.otHours.toStringAsFixed(1)}h',
  122. style: const TextStyle(color: AppColors.textSecondary, fontSize: 12)),
  123. const SizedBox(height: 4),
  124. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  125. Text(item.applicantName, style: const TextStyle(color: AppColors.textHint, fontSize: 11)),
  126. Text(du.DateUtils.formatDate(item.otDate), style: const TextStyle(color: AppColors.textHint, fontSize: 11)),
  127. ]),
  128. ]),
  129. );
  130. }
  131. }