| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- import 'package:tdesign_flutter/tdesign_flutter.dart';
- import 'package:easy_refresh/easy_refresh.dart';
- import '../../core/theme/app_colors_extension.dart';
- import '../../core/utils/date_utils.dart' as du;
- import '../../core/utils/responsive.dart';
- import '../../shared/widgets/list_card.dart';
- import '../../shared/widgets/empty_state.dart';
- import '../../shared/widgets/app_skeletons.dart';
- import '../../shared/widgets/list_footer.dart';
- import '../../core/i18n/app_localizations.dart';
- import 'expense_list_controller.dart';
- import 'expense_model.dart';
- import 'expense_api.dart';
- class ExpenseListPage extends ConsumerStatefulWidget {
- const ExpenseListPage({super.key});
- @override
- ConsumerState<ExpenseListPage> createState() => _ExpenseListPageState();
- }
- class _ExpenseListPageState extends ConsumerState<ExpenseListPage> {
- final _keywordCtrl = TextEditingController();
- final _startDateCtrl = TextEditingController();
- final _endDateCtrl = TextEditingController();
- late final EasyRefreshController _refreshCtrl;
- bool _firstBuild = true;
- bool _invalidateDone = false;
- @override
- void initState() {
- super.initState();
- final now = DateTime.now();
- _startDateCtrl.text =
- '${now.year}-${now.month.toString().padLeft(2, '0')}-01';
- _endDateCtrl.text =
- '${now.year}-${now.month.toString().padLeft(2, '0')}-${_daysInMonth(now.year, now.month).toString().padLeft(2, '0')}';
- _refreshCtrl = EasyRefreshController();
- WidgetsBinding.instance.addPostFrameCallback((_) {
- ref.read(expenseDateStartProvider.notifier).state = DateTime(
- now.year,
- now.month,
- 1,
- );
- ref.read(expenseDateEndProvider.notifier).state = DateTime(
- now.year,
- now.month,
- _daysInMonth(now.year, now.month),
- );
- ref.invalidate(expenseMyListProvider(''));
- _invalidateDone = true;
- setState(() {});
- });
- }
- @override
- void dispose() {
- _keywordCtrl.dispose();
- _startDateCtrl.dispose();
- _endDateCtrl.dispose();
- _refreshCtrl.dispose();
- super.dispose();
- }
- void _pickDate(TextEditingController ctrl) {
- FocusManager.instance.primaryFocus?.unfocus();
- final l10n = AppLocalizations.of(context);
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final now = DateTime.now();
- TDPicker.showDatePicker(
- context,
- title: l10n.get('selectDate'),
- backgroundColor: colors.bgCard,
- useYear: true,
- useMonth: true,
- useDay: true,
- useHour: false,
- useMinute: false,
- useSecond: false,
- useWeekDay: false,
- dateStart: const [2020, 1, 1],
- dateEnd: [now.year + 1, 12, 31],
- initialDate: [now.year, now.month, now.day],
- onConfirm: (selected) {
- ctrl.text =
- '${selected['year']}-${selected['month'].toString().padLeft(2, '0')}-${selected['day'].toString().padLeft(2, '0')}';
- setState(() {});
- Navigator.of(context).pop();
- },
- );
- }
- int _daysInMonth(int year, int month) => DateTime(year, month + 1, 0).day;
- void _applyFilter() {
- FocusManager.instance.primaryFocus?.unfocus();
- _refreshCtrl.callRefresh();
- }
- Future<void> _doRefresh() async {
- ref.read(expenseKeywordProvider.notifier).state = _keywordCtrl.text.trim();
- ref
- .read(expenseDateStartProvider.notifier)
- .state = _startDateCtrl.text.isNotEmpty
- ? DateTime.tryParse(_startDateCtrl.text)
- : null;
- ref
- .read(expenseDateEndProvider.notifier)
- .state = _endDateCtrl.text.isNotEmpty
- ? DateTime.tryParse(_endDateCtrl.text)
- : null;
- ref.read(expenseRefreshProvider.notifier).state++;
- }
- Widget _dateChip(
- TextEditingController ctrl,
- String hint,
- TDThemeData tdTheme,
- AppColorsExtension colors,
- ) {
- final text = ctrl.text;
- return Container(
- height: 40,
- padding: const EdgeInsets.symmetric(horizontal: 12),
- decoration: BoxDecoration(
- color: colors.bgSecondaryContainer,
- borderRadius: BorderRadius.circular(20),
- border: Border.all(color: tdTheme.componentStrokeColor),
- ),
- child: Row(
- children: [
- Icon(Icons.calendar_today, size: 16, color: colors.textSecondary),
- const SizedBox(width: 6),
- Expanded(
- child: Text(
- text.isNotEmpty ? text : hint,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- style: TextStyle(
- fontSize: 14,
- color: text.isNotEmpty
- ? colors.textPrimary
- : colors.textSecondary,
- ),
- ),
- ),
- if (text.isNotEmpty)
- GestureDetector(
- onTap: () {
- ctrl.clear();
- setState(() {});
- },
- child: Icon(Icons.close, size: 18, color: colors.textSecondary),
- ),
- ],
- ),
- );
- }
- @override
- Widget build(BuildContext context) {
- final l10n = AppLocalizations.of(context);
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final tdTheme = TDTheme.of(context);
- if (_invalidateDone) {
- ref.listen(expenseMyListProvider(''), (prev, next) {
- if (_firstBuild && !next.isLoading && !next.isReloading) {
- setState(() => _firstBuild = false);
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (mounted) setState(() {});
- });
- }
- });
- }
- return Stack(
- children: [
- Column(
- children: [
- Container(
- decoration: BoxDecoration(
- color: colors.bgCard,
- border: Border(
- bottom: BorderSide(color: tdTheme.componentStrokeColor),
- ),
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(12, 8, 12, 0),
- child: Row(
- children: [
- Expanded(
- child: GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () => _pickDate(_startDateCtrl),
- child: _dateChip(
- _startDateCtrl,
- l10n.get('filterStartDate'),
- tdTheme,
- colors,
- ),
- ),
- ),
- const SizedBox(width: 8),
- Text(
- '—',
- style: TextStyle(
- fontSize: 14,
- color: colors.textSecondary,
- ),
- ),
- const SizedBox(width: 8),
- Expanded(
- child: GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () => _pickDate(_endDateCtrl),
- child: _dateChip(
- _endDateCtrl,
- l10n.get('filterEndDate'),
- tdTheme,
- colors,
- ),
- ),
- ),
- ],
- ),
- ),
- const SizedBox(height: 8),
- Padding(
- padding: const EdgeInsets.fromLTRB(12, 0, 12, 8),
- child: Row(
- children: [
- Expanded(
- child: Container(
- height: 40,
- padding: const EdgeInsets.symmetric(horizontal: 16),
- decoration: BoxDecoration(
- color: colors.bgSecondaryContainer,
- borderRadius: BorderRadius.circular(20),
- border: Border.all(
- color: tdTheme.componentStrokeColor,
- ),
- ),
- child: Row(
- children: [
- Expanded(
- child: TextField(
- controller: _keywordCtrl,
- style: TextStyle(
- fontSize: 14,
- color: colors.textPrimary,
- ),
- decoration: InputDecoration(
- hintText: l10n.get('searchExpense'),
- hintStyle: TextStyle(
- fontSize: 14,
- color: colors.textSecondary,
- ),
- border: InputBorder.none,
- isCollapsed: true,
- ),
- onChanged: (_) => setState(() {}),
- ),
- ),
- if (_keywordCtrl.text.isNotEmpty)
- GestureDetector(
- onTap: () {
- _keywordCtrl.clear();
- setState(() {});
- _applyFilter();
- },
- child: Icon(
- Icons.close,
- size: 18,
- color: colors.textSecondary,
- ),
- ),
- ],
- ),
- ),
- ),
- const SizedBox(width: 8),
- GestureDetector(
- onTap: () {
- final dir = ref.read(expenseSortDirProvider);
- ref.read(expenseSortDirProvider.notifier).state =
- dir == 'ASC' ? 'DESC' : 'ASC';
- _applyFilter();
- },
- child: Container(
- width: 40,
- height: 40,
- decoration: BoxDecoration(
- color: colors.primary,
- borderRadius: BorderRadius.circular(20),
- ),
- child: Center(
- child: Icon(
- ref.watch(expenseSortDirProvider) == 'ASC'
- ? Icons.arrow_upward
- : Icons.arrow_downward,
- color: Colors.white,
- size: 20,
- ),
- ),
- ),
- ),
- const SizedBox(width: 8),
- GestureDetector(
- onTap: _applyFilter,
- child: Container(
- width: 40,
- height: 40,
- decoration: BoxDecoration(
- color: colors.primary,
- borderRadius: BorderRadius.circular(20),
- ),
- child: const Icon(
- Icons.search,
- color: Colors.white,
- size: 22,
- ),
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- Expanded(
- child: Container(
- color: colors.bgPage,
- child: _firstBuild
- ? const Center(child: SkeletonLoadingList())
- : _ExpenseListContent(
- refreshCtrl: _refreshCtrl,
- onRefresh: _doRefresh,
- ),
- ),
- ),
- ],
- ),
- Positioned(
- right: 16,
- bottom: 16,
- child: FloatingActionButton(
- onPressed: () => context.push('/report/expense-detail'),
- backgroundColor: colors.primary,
- shape: const CircleBorder(),
- child: const Icon(Icons.bar_chart, color: Colors.white),
- ),
- ),
- ],
- );
- }
- }
- class _ExpenseListContent extends ConsumerWidget {
- final EasyRefreshController refreshCtrl;
- final Future<void> Function() onRefresh;
- const _ExpenseListContent({
- required this.refreshCtrl,
- required this.onRefresh,
- });
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final r = ResponsiveHelper.of(context);
- final itemsAsync = ref.watch(expenseMyListProvider(''));
- if (itemsAsync.isLoading && !itemsAsync.hasValue) {
- return Center(
- child: ConstrainedBox(
- constraints: BoxConstraints(maxWidth: r.listMaxWidth),
- child: const SkeletonLoadingList(),
- ),
- );
- }
- return Center(
- child: ConstrainedBox(
- constraints: BoxConstraints(maxWidth: r.listMaxWidth),
- child: EasyRefresh(
- controller: refreshCtrl,
- header: TDRefreshHeader(),
- onRefresh: onRefresh,
- child: _buildContent(itemsAsync, context, ref),
- ),
- ),
- );
- }
- Widget _buildContent(
- AsyncValue<List<ExpenseModel>> itemsAsync,
- BuildContext context,
- WidgetRef ref,
- ) {
- final l10n = AppLocalizations.of(context);
- if (itemsAsync.isReloading) {
- final oldItems = itemsAsync.valueOrNull ?? [];
- if (oldItems.isEmpty) {
- return ListView(
- children: [
- const SizedBox(height: 120),
- EmptyState(message: l10n.get('noExpenses')),
- ],
- );
- }
- return ListView.builder(
- padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
- itemCount: oldItems.length,
- itemBuilder: (_, i) {
- final item = oldItems[i];
- final applicant = item.deptName.isNotEmpty
- ? '${item.applicantName} · ${item.deptName}'
- : item.applicantName;
- final card = ListCard(
- cardNo: item.expenseNo,
- amount: '¥${item.totalAmount.toStringAsFixed(2)}',
- subAmount: item.totalAmtnSh > 0
- ? '¥${item.totalAmtnSh.toStringAsFixed(2)}'
- : null,
- applicant: applicant,
- description: item.purpose,
- date: du.DateUtils.formatDate(item.expenseDate ?? item.createTime),
- statusTag: _buildStatusTags(item, l10n, context),
- onTap: () {
- context.push('/expense/detail/${item.expenseNo}');
- },
- );
- return Padding(
- padding: const EdgeInsets.only(bottom: 16),
- child: card,
- );
- },
- );
- }
- if (itemsAsync.hasError) {
- return ListView(
- children: [
- const SizedBox(height: 120),
- EmptyState(message: l10n.get('loadFailed')),
- ],
- );
- }
- final items = itemsAsync.requireValue;
- if (items.isEmpty) {
- return ListView(
- children: [
- const SizedBox(height: 120),
- EmptyState(message: l10n.get('noExpenses')),
- ],
- );
- }
- return ListView.builder(
- padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
- itemCount: items.length + 1,
- itemBuilder: (_, i) {
- if (i == items.length) return ListFooter(itemCount: items.length);
- final item = items[i];
- final applicant = item.deptName.isNotEmpty
- ? '${item.applicantName} · ${item.deptName}'
- : item.applicantName;
- final card = ListCard(
- cardNo: item.expenseNo,
- amount: '¥${item.totalAmount.toStringAsFixed(2)}',
- subAmount: item.totalAmtnSh > 0
- ? '¥${item.totalAmtnSh.toStringAsFixed(2)}'
- : null,
- applicant: applicant,
- description: item.purpose,
- date: du.DateUtils.formatDate(item.expenseDate ?? item.createTime),
- statusTag: _buildStatusTags(item, l10n, context),
- onTap: () {
- context.push('/expense/detail/${item.expenseNo}');
- },
- );
- return Padding(padding: const EdgeInsets.only(bottom: 16), child: card);
- },
- );
- }
- /// 构建状态 tag,放在卡片底部日期旁边
- Widget _buildStatusTags(
- ExpenseModel item,
- AppLocalizations l10n,
- BuildContext context,
- ) {
- final tags = <Widget>[];
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final isDark = Theme.of(context).brightness == Brightness.dark;
- final tagBg = isDark ? colors.bgSecondaryContainer : Colors.grey.shade200;
- final tagFg = isDark ? colors.textSecondary : Colors.grey.shade600;
- if (item.isTransferred == 1) {
- tags.add(
- Container(
- padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
- decoration: BoxDecoration(
- color: tagBg,
- borderRadius: BorderRadius.circular(4),
- ),
- child: Text(
- l10n.get('statusTransferred'),
- style: TextStyle(fontSize: 11, color: tagFg),
- ),
- ),
- );
- tags.add(const SizedBox(width: 6));
- }
- if (item.isClosed == 1) {
- tags.add(
- Container(
- padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
- decoration: BoxDecoration(
- color: tagBg,
- borderRadius: BorderRadius.circular(4),
- ),
- child: Text(
- l10n.get('statusClosed'),
- style: TextStyle(fontSize: 11, color: tagFg),
- ),
- ),
- );
- }
- if (tags.isEmpty) return const SizedBox.shrink();
- return Row(mainAxisSize: MainAxisSize.min, children: tags);
- }
- }
|