expense_apply_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:tdesign_flutter/tdesign_flutter.dart';
  4. import 'package:go_router/go_router.dart';
  5. import '../shell/nav_bar_config.dart';
  6. import '../../core/utils/responsive.dart';
  7. import '../../shared/widgets/form_section.dart';
  8. import '../../shared/widgets/form_field_row.dart';
  9. import '../../shared/widgets/action_bar.dart';
  10. import 'expense_apply_controller.dart';
  11. import '../../core/i18n/app_localizations.dart';
  12. import 'expense_model.dart';
  13. import '../../core/theme/app_colors.dart';
  14. import '../../core/theme/app_colors_extension.dart';
  15. class ExpenseApplyPage extends ConsumerStatefulWidget {
  16. final String? editId;
  17. const ExpenseApplyPage({super.key, this.editId});
  18. @override
  19. ConsumerState<ExpenseApplyPage> createState() => _ExpenseApplyPageState();
  20. }
  21. class _ExpenseApplyPageState extends ConsumerState<ExpenseApplyPage> {
  22. final _remarkController = TextEditingController();
  23. final _purposeController = TextEditingController();
  24. final _bankNameController = TextEditingController(text: '中国银行');
  25. final _accountNameController = TextEditingController(text: '张三');
  26. @override
  27. void dispose() {
  28. _remarkController.dispose();
  29. _purposeController.dispose();
  30. _bankNameController.dispose();
  31. _accountNameController.dispose();
  32. super.dispose();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. final controller = ref.watch(expenseApplyProvider(widget.editId).notifier);
  37. final state = ref.watch(expenseApplyProvider(widget.editId));
  38. final r = ResponsiveHelper.of(context);
  39. final l10n = AppLocalizations.of(context);
  40. ref
  41. .read(navBarConfigProvider.notifier)
  42. .update(
  43. NavBarConfig(
  44. title: widget.editId != null
  45. ? l10n.get('editExpense')
  46. : l10n.get('expenseApply'),
  47. showBack: true,
  48. onBack: () => context.pop(),
  49. ),
  50. );
  51. return Column(
  52. children: [
  53. Expanded(
  54. child: Align(
  55. alignment: Alignment.topCenter,
  56. child: ConstrainedBox(
  57. constraints: BoxConstraints(maxWidth: r.formMaxWidth),
  58. child: SingleChildScrollView(
  59. padding: const EdgeInsets.all(16),
  60. child: Column(
  61. children: [
  62. _buildImportLink(),
  63. const SizedBox(height: 16),
  64. _buildBasicInfoSection(controller, state),
  65. const SizedBox(height: 16),
  66. _buildAccountSection(controller, state),
  67. const SizedBox(height: 16),
  68. _buildDetailSection(controller, state),
  69. const SizedBox(height: 16),
  70. _buildInvoiceSection(controller, state),
  71. ],
  72. ),
  73. ),
  74. ),
  75. ),
  76. ),
  77. _buildBottomButtons(controller, state),
  78. ],
  79. );
  80. }
  81. Widget _buildImportLink() {
  82. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  83. final l10n = AppLocalizations.of(context);
  84. return GestureDetector(
  85. onTap: () {
  86. TDToast.showText(l10n.get('expenseApplyImport'), context: context);
  87. },
  88. child: Container(
  89. height: 44,
  90. decoration: BoxDecoration(
  91. color: colors.primaryLight,
  92. borderRadius: BorderRadius.circular(8),
  93. ),
  94. child: Row(
  95. mainAxisAlignment: MainAxisAlignment.center,
  96. children: [
  97. Icon(Icons.download, size: 14, color: colors.primary),
  98. const SizedBox(width: 8),
  99. Text(
  100. l10n.get('importApprovedPreApp'),
  101. style: TextStyle(
  102. fontSize: AppFontSizes.body,
  103. color: colors.primary,
  104. ),
  105. ),
  106. ],
  107. ),
  108. ),
  109. );
  110. }
  111. Widget _buildBasicInfoSection(
  112. ExpenseApplyController controller,
  113. ExpenseApplyState state,
  114. ) {
  115. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  116. final l10n = AppLocalizations.of(context);
  117. final expense = state.expense;
  118. return FormSection(
  119. title: l10n.get('basicInfo'),
  120. children: [
  121. FormFieldRow(
  122. label: l10n.get('expenseReason'),
  123. hint: l10n.get('enterExpenseReason'),
  124. ),
  125. FormFieldRow(
  126. label: l10n.get('relatedProject'),
  127. value: expense.projectName.isNotEmpty ? expense.projectName : null,
  128. hint: l10n.get('selectProject'),
  129. onTap: () {
  130. TDToast.showText(l10n.get('projectSelection'), context: context);
  131. },
  132. ),
  133. FormFieldRow(
  134. label: l10n.get('budgetSubject'),
  135. value: expense.budgetSubjectId.isNotEmpty
  136. ? expense.budgetSubjectId
  137. : null,
  138. hint: l10n.get('selectSubject'),
  139. onTap: () {
  140. TDToast.showText(
  141. l10n.get('budgetSubjectSelection'),
  142. context: context,
  143. );
  144. },
  145. ),
  146. FormFieldRow(
  147. label: l10n.get('costCenter'),
  148. value: expense.costCenterId.isNotEmpty ? expense.costCenterId : null,
  149. hint: l10n.get('selectCostCenter'),
  150. onTap: () {
  151. TDToast.showText(l10n.get('costCenterSelection'), context: context);
  152. },
  153. ),
  154. Container(
  155. height: 44,
  156. padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 0),
  157. child: Row(
  158. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  159. children: [
  160. Text(
  161. l10n.get('totalExpense'),
  162. style: TextStyle(
  163. fontSize: AppFontSizes.body,
  164. color: colors.textSecondary,
  165. ),
  166. ),
  167. Text(
  168. '¥${expense.totalAmount.toStringAsFixed(2)}',
  169. style: TextStyle(
  170. fontSize: AppFontSizes.subtitle,
  171. fontWeight: FontWeight.w700,
  172. color: colors.amountPrimary,
  173. ),
  174. ),
  175. ],
  176. ),
  177. ),
  178. ],
  179. );
  180. }
  181. Widget _buildAccountSection(
  182. ExpenseApplyController controller,
  183. ExpenseApplyState state,
  184. ) {
  185. final l10n = AppLocalizations.of(context);
  186. return FormSection(
  187. title: l10n.get('receiptAccount'),
  188. children: [
  189. FormFieldRow(
  190. label: l10n.get('bankName'),
  191. value: _bankNameController.text.isNotEmpty
  192. ? _bankNameController.text
  193. : null,
  194. hint: l10n.get('selectBank'),
  195. onTap: () {
  196. TDToast.showText(l10n.get('bankSelection'), context: context);
  197. },
  198. ),
  199. FormFieldRow(
  200. label: l10n.get('accountName'),
  201. value: _accountNameController.text,
  202. readOnly: true,
  203. showArrow: false,
  204. ),
  205. FormFieldRow(
  206. label: l10n.get('bankAccount'),
  207. hint: l10n.get('enterBankAccount'),
  208. onTap: () {
  209. TDToast.showText(l10n.get('bankAccountInput'), context: context);
  210. },
  211. ),
  212. ],
  213. );
  214. }
  215. Widget _buildDetailSection(
  216. ExpenseApplyController controller,
  217. ExpenseApplyState state,
  218. ) {
  219. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  220. final l10n = AppLocalizations.of(context);
  221. return FormSection(
  222. title: l10n.get('expenseDetails'),
  223. showAction: state.expense.details.isNotEmpty,
  224. actionText: l10n.get('add'),
  225. onActionTap: () => _showAddDetailDialog(controller),
  226. children: [
  227. if (state.expense.details.isEmpty) ...[
  228. GestureDetector(
  229. onTap: () => _showAddDetailDialog(controller),
  230. child: Container(
  231. padding: const EdgeInsets.symmetric(vertical: 12),
  232. decoration: BoxDecoration(
  233. border: Border.all(
  234. color: colors.border,
  235. strokeAlign: BorderSide.strokeAlignInside,
  236. ),
  237. borderRadius: BorderRadius.circular(4),
  238. color: colors.bgPage,
  239. ),
  240. child: Row(
  241. mainAxisAlignment: MainAxisAlignment.center,
  242. children: [
  243. Icon(Icons.add, size: 16, color: colors.primary),
  244. const SizedBox(width: 4),
  245. Text(
  246. l10n.get('addExpenseDetail'),
  247. style: TextStyle(
  248. fontSize: AppFontSizes.body,
  249. color: colors.primary,
  250. ),
  251. ),
  252. ],
  253. ),
  254. ),
  255. ),
  256. ] else
  257. ...state.expense.details.asMap().entries.map((entry) {
  258. final d = entry.value;
  259. return Container(
  260. height: 38,
  261. padding: const EdgeInsets.symmetric(vertical: 4),
  262. child: Row(
  263. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  264. children: [
  265. Row(
  266. children: [
  267. Icon(
  268. Icons.receipt_long,
  269. size: 14,
  270. color: colors.textSecondary,
  271. ),
  272. const SizedBox(width: 8),
  273. Text(
  274. d.expenseDesc,
  275. style: TextStyle(
  276. fontSize: AppFontSizes.body,
  277. color: colors.textPrimary,
  278. ),
  279. ),
  280. ],
  281. ),
  282. Row(
  283. mainAxisSize: MainAxisSize.min,
  284. children: [
  285. Text(
  286. '¥${d.totalAmount.toStringAsFixed(2)}',
  287. style: TextStyle(
  288. fontSize: AppFontSizes.body,
  289. fontWeight: FontWeight.w500,
  290. color: colors.amountPrimary,
  291. ),
  292. ),
  293. const SizedBox(width: 8),
  294. GestureDetector(
  295. onTap: () {
  296. controller.removeDetail(entry.key);
  297. controller.recalculateAmount();
  298. },
  299. child: Icon(
  300. Icons.close,
  301. size: 16,
  302. color: colors.textPlaceholder,
  303. ),
  304. ),
  305. ],
  306. ),
  307. ],
  308. ),
  309. );
  310. }),
  311. if (state.expense.details.isNotEmpty) ...[
  312. Container(height: 1, color: colors.border),
  313. Container(
  314. height: 36,
  315. padding: const EdgeInsets.symmetric(vertical: 8),
  316. child: Row(
  317. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  318. children: [
  319. Text(
  320. l10n.get('total'),
  321. style: TextStyle(
  322. fontSize: AppFontSizes.body,
  323. fontWeight: FontWeight.w600,
  324. color: colors.textPrimary,
  325. ),
  326. ),
  327. Text(
  328. '¥${state.expense.totalAmount.toStringAsFixed(2)}',
  329. style: TextStyle(
  330. fontSize: AppFontSizes.subtitle,
  331. fontWeight: FontWeight.w700,
  332. color: colors.amountPrimary,
  333. ),
  334. ),
  335. ],
  336. ),
  337. ),
  338. ],
  339. ],
  340. );
  341. }
  342. Widget _buildInvoiceSection(
  343. ExpenseApplyController controller,
  344. ExpenseApplyState state,
  345. ) {
  346. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  347. final l10n = AppLocalizations.of(context);
  348. return FormSection(
  349. title: l10n.get('invoiceUpload'),
  350. children: [
  351. Text(
  352. l10n.get('maxInvoices'),
  353. style: TextStyle(
  354. fontSize: AppFontSizes.caption,
  355. color: colors.textPlaceholder,
  356. ),
  357. ),
  358. const SizedBox(height: 8),
  359. Wrap(
  360. spacing: 8,
  361. runSpacing: 8,
  362. children: List.generate(6, (i) {
  363. return Container(
  364. width: 80,
  365. height: 80,
  366. decoration: BoxDecoration(
  367. color: colors.bgPage,
  368. borderRadius: BorderRadius.circular(4),
  369. border: Border.all(
  370. color: colors.border,
  371. strokeAlign: BorderSide.strokeAlignInside,
  372. ),
  373. ),
  374. child: i == 0
  375. ? Center(
  376. child: Icon(
  377. Icons.add,
  378. size: 24,
  379. color: colors.textPlaceholder,
  380. ),
  381. )
  382. : const SizedBox.shrink(),
  383. );
  384. }),
  385. ),
  386. ],
  387. );
  388. }
  389. Widget _buildBottomButtons(
  390. ExpenseApplyController controller,
  391. ExpenseApplyState state,
  392. ) {
  393. final l10n = AppLocalizations.of(context);
  394. return ActionBar(
  395. leftLabel: l10n.get('reset'),
  396. centerLabel: l10n.get('saveDraft'),
  397. rightLabel: l10n.get('submitApproval'),
  398. onLeftTap: () {
  399. setState(() {
  400. _purposeController.clear();
  401. _remarkController.clear();
  402. });
  403. },
  404. onCenterTap: state.isSubmitting
  405. ? null
  406. : () async {
  407. await controller.saveDraft();
  408. if (!mounted) return;
  409. context.pop();
  410. },
  411. onRightTap: state.isSubmitting
  412. ? null
  413. : () async {
  414. final ok = await controller.submit();
  415. if (!mounted) return;
  416. if (ok) context.pop();
  417. },
  418. showLeft: true,
  419. );
  420. }
  421. void _showAddDetailDialog(ExpenseApplyController controller) {
  422. final l10n = AppLocalizations.of(context);
  423. final nameCtrl = TextEditingController();
  424. final amountCtrl = TextEditingController();
  425. final descCtrl = TextEditingController();
  426. showDialog(
  427. context: context,
  428. builder: (_) => TDAlertDialog(
  429. title: l10n.get('addDetail'),
  430. contentWidget: Column(
  431. mainAxisSize: MainAxisSize.min,
  432. children: [
  433. TDInput(controller: nameCtrl, hintText: l10n.get('expenseName')),
  434. const SizedBox(height: 8),
  435. TDInput(
  436. controller: amountCtrl,
  437. hintText: l10n.get('amount'),
  438. inputType: TextInputType.number,
  439. ),
  440. const SizedBox(height: 8),
  441. TDInput(controller: descCtrl, hintText: l10n.get('description')),
  442. ],
  443. ),
  444. leftBtn: TDDialogButtonOptions(
  445. title: l10n.get('cancel'),
  446. action: () => Navigator.pop(context),
  447. ),
  448. rightBtn: TDDialogButtonOptions(
  449. title: l10n.get('add'),
  450. action: () {
  451. final amount = double.tryParse(amountCtrl.text) ?? 0.0;
  452. controller.addDetail(
  453. ExpenseDetailModel(
  454. id: DateTime.now().millisecondsSinceEpoch.toString(),
  455. expenseId: '',
  456. expenseDate: DateTime.now(),
  457. expenseType: '',
  458. expenseDesc: nameCtrl.text,
  459. amount: amount,
  460. totalAmount: amount,
  461. remark: descCtrl.text,
  462. ),
  463. );
  464. controller.recalculateAmount();
  465. Navigator.pop(context);
  466. },
  467. ),
  468. ),
  469. );
  470. }
  471. }