expense_detail_dialog.dart 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import '../../../core/i18n/app_localizations.dart';
  6. import '../../../core/theme/app_colors.dart';
  7. import '../../../core/theme/app_colors_extension.dart';
  8. import '../../../core/data/mock_api_data.dart';
  9. import '../expense_api.dart';
  10. import '../../../shared/widgets/attachment_picker.dart';
  11. /// 报销明细输入数据。
  12. class ExpenseDetailInputData {
  13. final String category;
  14. final String categoryName;
  15. final String acctSubjectId;
  16. final String acctSubjectName;
  17. final String purpose;
  18. final double amount; // 含税金额
  19. final double taxRate;
  20. final String projectId;
  21. final String projectName;
  22. final String costDeptId;
  23. final String costDeptName;
  24. final String customerVendorId;
  25. final String customerVendorName;
  26. final double approvedAmount;
  27. final double offsetAmount;
  28. final String bankName;
  29. final String bankAccountName;
  30. final String bankAccount;
  31. final String remark;
  32. final List<String> attachmentPaths;
  33. final String sqMan;
  34. final String sqManName;
  35. final String aeNo;
  36. final String aeDd;
  37. const ExpenseDetailInputData({
  38. required this.category,
  39. required this.categoryName,
  40. required this.acctSubjectId,
  41. required this.acctSubjectName,
  42. required this.purpose,
  43. required this.amount,
  44. required this.taxRate,
  45. this.projectId = '',
  46. this.projectName = '',
  47. this.costDeptId = '',
  48. this.costDeptName = '',
  49. this.customerVendorId = '',
  50. this.customerVendorName = '',
  51. this.approvedAmount = 0.0,
  52. this.offsetAmount = 0.0,
  53. this.bankName = '',
  54. this.bankAccountName = '',
  55. this.bankAccount = '',
  56. this.remark = '',
  57. this.attachmentPaths = const [],
  58. this.sqMan = '',
  59. this.sqManName = '',
  60. this.aeNo = '',
  61. this.aeDd = '',
  62. });
  63. }
  64. /// 报销明细编辑弹窗。
  65. ///
  66. /// 使用 [TDSlidePopupRoute] 从底部滑出,卡片化展示表单字段。
  67. /// 参照 ExpenseApplyCreatePage 的 ExpenseDetailDialog 样式。
  68. class ExpenseDetailDialog extends StatefulWidget {
  69. final List<CostCategory> categories;
  70. final List<Project> projects;
  71. final List<CostDept> costDepts;
  72. final List<CustomerVendor> customers;
  73. final List<EmployeeItem> employees;
  74. final AppLocalizations l10n;
  75. final ExpenseDetailInputData? initialData;
  76. final Future<bool> Function()? checkAttachHealth;
  77. const ExpenseDetailDialog({
  78. super.key,
  79. required this.categories,
  80. required this.projects,
  81. required this.costDepts,
  82. required this.customers,
  83. required this.employees,
  84. required this.l10n,
  85. this.initialData,
  86. this.checkAttachHealth,
  87. });
  88. /// 显示弹窗,返回 [ExpenseDetailInputData] 或 `null`(取消时)。
  89. static Future<ExpenseDetailInputData?> show(
  90. BuildContext context, {
  91. required List<CostCategory> categories,
  92. required List<Project> projects,
  93. required List<CostDept> costDepts,
  94. required List<CustomerVendor> customers,
  95. required List<EmployeeItem> employees,
  96. required AppLocalizations l10n,
  97. ExpenseDetailInputData? initialData,
  98. Future<bool> Function()? checkAttachHealth,
  99. }) {
  100. FocusScope.of(context).unfocus();
  101. return Navigator.push<ExpenseDetailInputData>(
  102. context,
  103. TDSlidePopupRoute<ExpenseDetailInputData>(
  104. slideTransitionFrom: SlideTransitionFrom.bottom,
  105. isDismissible: true,
  106. builder: (_) => ExpenseDetailDialog(
  107. categories: categories,
  108. projects: projects,
  109. costDepts: costDepts,
  110. customers: customers,
  111. employees: employees,
  112. l10n: l10n,
  113. initialData: initialData,
  114. checkAttachHealth: checkAttachHealth,
  115. ),
  116. ),
  117. );
  118. }
  119. @override
  120. State<ExpenseDetailDialog> createState() => _ExpenseDetailDialogState();
  121. }
  122. class _ExpenseDetailDialogState extends State<ExpenseDetailDialog> {
  123. late String _cat;
  124. late TextEditingController _descCtrl;
  125. late TextEditingController _amountCtrl;
  126. CustomerVendor? _selCustomer;
  127. late TextEditingController _approvedAmountCtrl;
  128. late TextEditingController _remarkCtrl;
  129. late TextEditingController _bankNameCtrl;
  130. late TextEditingController _bankAccountNameCtrl;
  131. late TextEditingController _bankAccountCtrl;
  132. double _taxRate = 0;
  133. Project? _selProject;
  134. CostDept? _selDept;
  135. EmployeeItem? _selEmployee;
  136. late final AttachmentPickerController _attachmentCtrl;
  137. final ScrollController _scrollCtrl = ScrollController();
  138. final _remarkFocus = FocusNode();
  139. final _bankNameFocus = FocusNode();
  140. final _bankAccountNameFocus = FocusNode();
  141. final _bankAccountFocus = FocusNode();
  142. bool _attachAvailable = false;
  143. void _ensureVisible(FocusNode node) {
  144. if (!node.hasFocus) return;
  145. _doEnsureVisible(node, 0, -1);
  146. }
  147. void _doEnsureVisible(FocusNode node, int attempt, double lastInsets) {
  148. if (attempt >= 15) return;
  149. WidgetsBinding.instance.addPostFrameCallback((_) {
  150. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  151. final insets = MediaQuery.of(context).viewInsets.bottom;
  152. if (insets != lastInsets) {
  153. _doEnsureVisible(node, attempt + 1, insets);
  154. return;
  155. }
  156. final ctx = node.context;
  157. if (ctx == null) return;
  158. Future.delayed(const Duration(milliseconds: 500), () {
  159. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  160. Scrollable.ensureVisible(
  161. ctx,
  162. alignment: 0.5,
  163. duration: const Duration(milliseconds: 300),
  164. );
  165. });
  166. });
  167. }
  168. static const _taxOptions = [0, 6, 9, 13];
  169. static const _taxLabels = ['0%', '6%', '9%', '13%'];
  170. List<CostCategory> get _cats => widget.categories;
  171. AppLocalizations get _l10n => widget.l10n;
  172. CostCategory get _selCat => _cats.firstWhere((c) => c.code == _cat);
  173. bool get _isEdit => widget.initialData != null;
  174. @override
  175. void initState() {
  176. super.initState();
  177. final d = widget.initialData;
  178. _cat = d != null
  179. ? (_cats.any((c) => c.code == d.category)
  180. ? d.category
  181. : _cats.first.code)
  182. : _cats.isNotEmpty
  183. ? _cats.first.code
  184. : 'other';
  185. _descCtrl = TextEditingController(text: d?.purpose ?? '');
  186. _amountCtrl = TextEditingController(
  187. text: d != null && d.amount > 0 ? d.amount.toStringAsFixed(2) : '',
  188. );
  189. if (d != null && d.customerVendorName.isNotEmpty) {
  190. _selCustomer = CustomerVendor(id: '', name: d.customerVendorName);
  191. }
  192. _approvedAmountCtrl = TextEditingController(
  193. text: d != null && d.approvedAmount > 0
  194. ? d.approvedAmount.toStringAsFixed(2)
  195. : '',
  196. );
  197. _remarkCtrl = TextEditingController(text: d?.remark ?? '');
  198. _bankNameCtrl = TextEditingController(text: d?.bankName ?? '');
  199. _bankAccountNameCtrl = TextEditingController(
  200. text: d?.bankAccountName ?? '',
  201. );
  202. _bankAccountCtrl = TextEditingController(text: d?.bankAccount ?? '');
  203. _taxRate = d?.taxRate ?? 0;
  204. if (d != null && d.sqMan.isNotEmpty && widget.employees.isNotEmpty) {
  205. final idx = widget.employees.indexWhere((e) => e.salNo == d.sqMan);
  206. if (idx >= 0) _selEmployee = widget.employees[idx];
  207. }
  208. if (d != null) {
  209. if (d.projectId.isNotEmpty && widget.projects.isNotEmpty) {
  210. _selProject = widget.projects.firstWhere(
  211. (p) => p.id.toString() == d.projectId,
  212. orElse: () => widget.projects.first,
  213. );
  214. }
  215. if (d.costDeptId.isNotEmpty && widget.costDepts.isNotEmpty) {
  216. _selDept = widget.costDepts.firstWhere(
  217. (dept) => dept.id == d.costDeptId,
  218. orElse: () => widget.costDepts.first,
  219. );
  220. }
  221. if (d.attachmentPaths.isNotEmpty) {
  222. // Restore attachments will be handled after build
  223. WidgetsBinding.instance.addPostFrameCallback((_) {
  224. _attachmentCtrl.restoreFromPaths(d.attachmentPaths);
  225. });
  226. }
  227. }
  228. _attachmentCtrl = AttachmentPickerController(maxCount: 9)
  229. ..addListener(() => setState(() {}));
  230. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  231. _bankNameFocus.addListener(() => _ensureVisible(_bankNameFocus));
  232. _bankAccountNameFocus.addListener(
  233. () => _ensureVisible(_bankAccountNameFocus),
  234. );
  235. _bankAccountFocus.addListener(() => _ensureVisible(_bankAccountFocus));
  236. _checkAttachHealth();
  237. }
  238. Future<void> _checkAttachHealth() async {
  239. if (widget.checkAttachHealth == null) return;
  240. if (mounted) setState(() => _attachAvailable = false);
  241. try {
  242. final ok = await widget.checkAttachHealth!();
  243. if (mounted) setState(() => _attachAvailable = ok);
  244. } catch (_) {
  245. if (mounted) setState(() => _attachAvailable = false);
  246. }
  247. }
  248. @override
  249. void dispose() {
  250. _descCtrl.dispose();
  251. _amountCtrl.dispose();
  252. _approvedAmountCtrl.dispose();
  253. _remarkCtrl.dispose();
  254. _bankNameCtrl.dispose();
  255. _bankAccountNameCtrl.dispose();
  256. _bankAccountCtrl.dispose();
  257. _attachmentCtrl.dispose();
  258. _scrollCtrl.dispose();
  259. _remarkFocus.dispose();
  260. _bankNameFocus.dispose();
  261. _bankAccountNameFocus.dispose();
  262. _bankAccountFocus.dispose();
  263. super.dispose();
  264. }
  265. void _confirm() {
  266. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  267. if (amount <= 0) {
  268. TDToast.showText(_l10n.get('amountPositive'), context: context);
  269. return;
  270. }
  271. Navigator.pop(
  272. context,
  273. ExpenseDetailInputData(
  274. category: _cat,
  275. categoryName: _l10n.get(_selCat.nameKey),
  276. acctSubjectId: _selCat.acctSubjectId,
  277. acctSubjectName: _selCat.acctSubjectName,
  278. purpose: '',
  279. amount: amount,
  280. taxRate: _taxRate,
  281. projectId: _selProject?.id.toString() ?? '',
  282. projectName: _selProject?.name ?? '',
  283. costDeptId: _selDept?.id ?? '',
  284. costDeptName: _selDept?.name ?? '',
  285. customerVendorId: _selCustomer?.id ?? '',
  286. customerVendorName: _selCustomer?.name ?? '',
  287. approvedAmount: double.tryParse(_approvedAmountCtrl.text) ?? 0,
  288. bankName: _bankNameCtrl.text.trim(),
  289. bankAccountName: _bankAccountNameCtrl.text.trim(),
  290. bankAccount: _bankAccountCtrl.text.trim(),
  291. remark: _remarkCtrl.text.trim(),
  292. attachmentPaths: _attachmentCtrl.toPathList(),
  293. sqMan: _selEmployee?.salNo ?? '',
  294. sqManName: _selEmployee?.name ?? '',
  295. aeNo: widget.initialData?.aeNo ?? '',
  296. aeDd: widget.initialData?.aeDd ?? '',
  297. ),
  298. );
  299. }
  300. double get _amountExclTax => _taxRate > 0
  301. ? (double.tryParse(_amountCtrl.text) ?? 0) / (1 + _taxRate / 100)
  302. : (double.tryParse(_amountCtrl.text) ?? 0);
  303. double get _taxAmount =>
  304. (double.tryParse(_amountCtrl.text) ?? 0) - _amountExclTax;
  305. @override
  306. Widget build(BuildContext context) {
  307. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  308. return AnimatedPadding(
  309. padding: EdgeInsets.only(
  310. bottom: MediaQuery.of(context).viewInsets.bottom,
  311. ),
  312. duration: const Duration(milliseconds: 200),
  313. child: SafeArea(
  314. child: ConstrainedBox(
  315. constraints: BoxConstraints(
  316. maxHeight: MediaQuery.of(context).size.height * 0.8,
  317. ),
  318. child: Container(
  319. decoration: BoxDecoration(
  320. color: colors.bgPage,
  321. borderRadius: const BorderRadius.vertical(
  322. top: Radius.circular(16),
  323. ),
  324. ),
  325. child: Column(
  326. mainAxisSize: MainAxisSize.min,
  327. crossAxisAlignment: CrossAxisAlignment.stretch,
  328. children: [
  329. _buildHeader(colors),
  330. Flexible(
  331. child: GestureDetector(
  332. onTap: () => FocusScope.of(context).unfocus(),
  333. behavior: HitTestBehavior.translucent,
  334. child: SingleChildScrollView(
  335. controller: _scrollCtrl,
  336. keyboardDismissBehavior:
  337. ScrollViewKeyboardDismissBehavior.manual,
  338. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  339. child: Column(
  340. mainAxisSize: MainAxisSize.min,
  341. crossAxisAlignment: CrossAxisAlignment.stretch,
  342. children: [
  343. if (_isEdit &&
  344. widget.initialData!.aeNo.isNotEmpty) ...[
  345. _buildAeInfoCard(colors),
  346. const SizedBox(height: 12),
  347. ],
  348. _buildCategoryCard(colors),
  349. const SizedBox(height: 12),
  350. _buildAcctSubjectCard(colors),
  351. const SizedBox(height: 12),
  352. _buildAmountCard(),
  353. const SizedBox(height: 12),
  354. _buildTaxRateCard(colors),
  355. if ((double.tryParse(_amountCtrl.text) ?? 0) > 0) ...[
  356. const SizedBox(height: 12),
  357. _buildCalcInfo(colors),
  358. ],
  359. const SizedBox(height: 12),
  360. _buildApprovedAmountCard(),
  361. const SizedBox(height: 12),
  362. _buildProjectCard(colors),
  363. const SizedBox(height: 12),
  364. _buildCostDeptCard(colors),
  365. const SizedBox(height: 12),
  366. _buildCustomerCard(colors),
  367. const SizedBox(height: 12),
  368. _buildEmployeeCard(colors),
  369. const SizedBox(height: 12),
  370. _buildBankInfoCard(colors),
  371. const SizedBox(height: 12),
  372. _buildRemarkInput(colors),
  373. if (widget.initialData == null) ...[
  374. const SizedBox(height: 12),
  375. _buildAttachmentCard(colors),
  376. ],
  377. ],
  378. ),
  379. ),
  380. ),
  381. ),
  382. Container(
  383. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  384. decoration: BoxDecoration(
  385. color: colors.bgCard,
  386. border: Border(
  387. top: BorderSide(color: colors.border, width: 0.5),
  388. ),
  389. ),
  390. child: _buildActions(),
  391. ),
  392. ],
  393. ),
  394. ),
  395. ),
  396. ),
  397. );
  398. }
  399. // ── 标题栏 ──
  400. Widget _buildHeader(AppColorsExtension colors) {
  401. return Column(
  402. mainAxisSize: MainAxisSize.min,
  403. children: [
  404. Center(
  405. child: Container(
  406. margin: const EdgeInsets.only(top: 8, bottom: 4),
  407. width: 36,
  408. height: 4,
  409. decoration: BoxDecoration(
  410. color: colors.border,
  411. borderRadius: BorderRadius.circular(2),
  412. ),
  413. ),
  414. ),
  415. Padding(
  416. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  417. child: Row(
  418. children: [
  419. const SizedBox(width: 28),
  420. Expanded(
  421. child: Center(
  422. child: Text(
  423. _l10n.get('addExpenseDetail'),
  424. style: TextStyle(
  425. fontSize: AppFontSizes.title,
  426. fontWeight: FontWeight.w600,
  427. color: colors.textPrimary,
  428. ),
  429. ),
  430. ),
  431. ),
  432. GestureDetector(
  433. onTap: () => Navigator.pop(context),
  434. child: Padding(
  435. padding: const EdgeInsets.all(4),
  436. child: Icon(
  437. Icons.close,
  438. size: 20,
  439. color: colors.textSecondary,
  440. ),
  441. ),
  442. ),
  443. ],
  444. ),
  445. ),
  446. ],
  447. );
  448. }
  449. // ── picker 卡片 ──
  450. Widget _pickerCard({
  451. required String label,
  452. required bool required,
  453. required String currentLabel,
  454. required List<String> labels,
  455. required ValueChanged<int> onSelected,
  456. required AppColorsExtension colors,
  457. VoidCallback? onClear,
  458. }) {
  459. final tdTheme = TDTheme.of(context);
  460. final hasValue = onClear != null;
  461. return GestureDetector(
  462. onTap: () {
  463. if (labels.isEmpty) {
  464. TDToast.showText(_l10n.get('noData'), context: context);
  465. return;
  466. }
  467. FocusManager.instance.primaryFocus?.unfocus();
  468. TDPicker.showMultiPicker(
  469. context,
  470. title: label,
  471. backgroundColor: colors.bgCard,
  472. data: [labels],
  473. onConfirm: (selected) {
  474. if (selected.isNotEmpty && selected[0] is int) {
  475. final idx = selected[0] as int;
  476. if (idx >= 0 && idx < labels.length) {
  477. Navigator.of(context).pop();
  478. onSelected(idx);
  479. }
  480. }
  481. },
  482. );
  483. },
  484. child: Container(
  485. padding: const EdgeInsets.only(
  486. left: 16,
  487. right: 10,
  488. top: 12,
  489. bottom: 12,
  490. ),
  491. decoration: BoxDecoration(
  492. color: tdTheme.bgColorContainer,
  493. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  494. border: Border.all(color: tdTheme.componentStrokeColor),
  495. ),
  496. child: Row(
  497. children: [
  498. TDText(
  499. label,
  500. maxLines: 1,
  501. overflow: TextOverflow.visible,
  502. font: tdTheme.fontBodyLarge,
  503. fontWeight: FontWeight.w400,
  504. style: const TextStyle(letterSpacing: 0),
  505. ),
  506. if (required)
  507. Padding(
  508. padding: const EdgeInsets.only(left: 4),
  509. child: TDText(
  510. '*',
  511. font: tdTheme.fontBodyLarge,
  512. fontWeight: FontWeight.w400,
  513. style: TextStyle(color: tdTheme.errorColor6),
  514. ),
  515. ),
  516. const SizedBox(width: 12),
  517. Expanded(
  518. child: Row(
  519. mainAxisAlignment: MainAxisAlignment.end,
  520. mainAxisSize: MainAxisSize.max,
  521. children: [
  522. Flexible(
  523. child: TDText(
  524. currentLabel,
  525. maxLines: 1,
  526. overflow: TextOverflow.ellipsis,
  527. font: tdTheme.fontBodyLarge,
  528. fontWeight: FontWeight.w400,
  529. textColor: tdTheme.textColorPrimary,
  530. textAlign: TextAlign.end,
  531. ),
  532. ),
  533. const SizedBox(width: 4),
  534. SizedBox(
  535. width: 18,
  536. height: 18,
  537. child: hasValue
  538. ? GestureDetector(
  539. onTap: onClear,
  540. child: Icon(
  541. Icons.close,
  542. size: 18,
  543. color: tdTheme.textColorPlaceholder,
  544. ),
  545. )
  546. : Icon(
  547. Icons.chevron_right,
  548. size: 18,
  549. color: tdTheme.textColorPlaceholder,
  550. ),
  551. ),
  552. ],
  553. ),
  554. ),
  555. ],
  556. ),
  557. ),
  558. );
  559. }
  560. // ── 费用类别 ──
  561. Widget _buildCategoryCard(AppColorsExtension colors) {
  562. return _pickerCard(
  563. label: _l10n.get('expenseCategory'),
  564. required: true,
  565. currentLabel: '${_selCat.code}/${_l10n.get(_selCat.nameKey)}',
  566. labels: _cats.map((c) => '${c.code}/${_l10n.get(c.nameKey)}').toList(),
  567. colors: colors,
  568. onSelected: (idx) => setState(() => _cat = _cats[idx].code),
  569. );
  570. }
  571. // ── 输入卡片(对齐 pickerCard 样式) ──
  572. Widget _inputCard({
  573. required String label,
  574. required bool required,
  575. required TextEditingController controller,
  576. required String hintText,
  577. required AppColorsExtension colors,
  578. TextInputType? keyboardType,
  579. List<TextInputFormatter>? inputFormatters,
  580. FocusNode? focusNode,
  581. }) {
  582. final tdTheme = TDTheme.of(context);
  583. final hasValue = controller.text.isNotEmpty;
  584. return Container(
  585. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  586. decoration: BoxDecoration(
  587. color: tdTheme.bgColorContainer,
  588. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  589. border: Border.all(color: tdTheme.componentStrokeColor),
  590. ),
  591. child: Row(
  592. children: [
  593. TDText(
  594. label,
  595. maxLines: 1,
  596. overflow: TextOverflow.visible,
  597. font: tdTheme.fontBodyLarge,
  598. fontWeight: FontWeight.w400,
  599. style: const TextStyle(letterSpacing: 0),
  600. ),
  601. if (required)
  602. Padding(
  603. padding: const EdgeInsets.only(left: 4),
  604. child: TDText(
  605. '*',
  606. font: tdTheme.fontBodyLarge,
  607. fontWeight: FontWeight.w400,
  608. style: TextStyle(color: tdTheme.errorColor6),
  609. ),
  610. ),
  611. const SizedBox(width: 12),
  612. Expanded(
  613. child: Row(
  614. mainAxisAlignment: MainAxisAlignment.end,
  615. mainAxisSize: MainAxisSize.max,
  616. children: [
  617. Flexible(
  618. child: TextField(
  619. controller: controller,
  620. focusNode: focusNode,
  621. textAlign: TextAlign.end,
  622. keyboardType: keyboardType,
  623. inputFormatters: inputFormatters,
  624. style: TextStyle(fontSize: 16, color: colors.textPrimary),
  625. decoration: InputDecoration(
  626. hintText: hintText,
  627. hintStyle: TextStyle(
  628. fontSize: 16,
  629. color: colors.textPlaceholder,
  630. ),
  631. border: InputBorder.none,
  632. isDense: true,
  633. contentPadding: EdgeInsets.zero,
  634. ),
  635. onChanged: (_) => setState(() {}),
  636. ),
  637. ),
  638. const SizedBox(width: 4),
  639. SizedBox(
  640. width: 18,
  641. height: 18,
  642. child: hasValue
  643. ? GestureDetector(
  644. onTap: () {
  645. controller.clear();
  646. setState(() {});
  647. },
  648. child: Icon(
  649. Icons.close,
  650. size: 18,
  651. color: tdTheme.textColorPlaceholder,
  652. ),
  653. )
  654. : null,
  655. ),
  656. ],
  657. ),
  658. ),
  659. ],
  660. ),
  661. );
  662. }
  663. // ── 含税金额 ──
  664. Widget _buildAmountCard() {
  665. return _inputCard(
  666. label: _l10n.get('amountInclTax'),
  667. required: true,
  668. controller: _amountCtrl,
  669. hintText: '>0',
  670. colors: Theme.of(context).extension<AppColorsExtension>()!,
  671. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  672. inputFormatters: [
  673. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  674. ],
  675. );
  676. }
  677. // ── 税率 ──
  678. Widget _buildTaxRateCard(AppColorsExtension colors) {
  679. final currentLabel = '${_taxRate.toStringAsFixed(0)}%';
  680. return _pickerCard(
  681. label: _l10n.get('taxRate'),
  682. required: false,
  683. currentLabel: currentLabel,
  684. labels: _taxLabels.toList(),
  685. colors: colors,
  686. onSelected: (idx) => setState(() {
  687. _taxRate = _taxOptions[idx].toDouble();
  688. }),
  689. );
  690. }
  691. // ── 计算信息 ──
  692. Widget _buildCalcInfo(AppColorsExtension colors) {
  693. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  694. if (amount <= 0) return const SizedBox.shrink();
  695. return Container(
  696. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  697. decoration: BoxDecoration(
  698. color: colors.primaryLight,
  699. borderRadius: BorderRadius.circular(8),
  700. ),
  701. child: Row(
  702. children: [
  703. Expanded(
  704. child: Text(
  705. '${_l10n.get('amountExcludingTax')}: ¥${_amountExclTax.toStringAsFixed(2)}',
  706. style: TextStyle(
  707. fontSize: AppFontSizes.body,
  708. color: colors.textSecondary,
  709. ),
  710. ),
  711. ),
  712. Text(
  713. '${_l10n.get('taxAmount')}: ¥${_taxAmount.toStringAsFixed(2)}',
  714. style: TextStyle(
  715. fontSize: AppFontSizes.body,
  716. color: colors.textSecondary,
  717. ),
  718. ),
  719. ],
  720. ),
  721. );
  722. }
  723. // ── 导入单据信息 ──
  724. Widget _buildAeInfoCard(AppColorsExtension colors) {
  725. final d = widget.initialData!;
  726. final tdTheme = TDTheme.of(context);
  727. return Container(
  728. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  729. decoration: _cardDecoration(tdTheme),
  730. child: Column(
  731. children: [
  732. _readOnlyRow(tdTheme, _l10n.get('expenseApplyNo'), d.aeNo),
  733. const SizedBox(height: 8),
  734. _readOnlyRow(tdTheme, _l10n.get('applyDate'), d.aeDd),
  735. ],
  736. ),
  737. );
  738. }
  739. Widget _readOnlyRow(TDThemeData tdTheme, String label, String value) {
  740. return Row(
  741. children: [
  742. TDText(
  743. label,
  744. font: tdTheme.fontBodyLarge,
  745. fontWeight: FontWeight.w400,
  746. style: const TextStyle(letterSpacing: 0),
  747. ),
  748. const SizedBox(width: 12),
  749. Expanded(
  750. child: TDText(
  751. value,
  752. maxLines: 1,
  753. overflow: TextOverflow.ellipsis,
  754. font: tdTheme.fontBodyLarge,
  755. fontWeight: FontWeight.w400,
  756. textColor: tdTheme.textColorPrimary,
  757. textAlign: TextAlign.end,
  758. ),
  759. ),
  760. ],
  761. );
  762. }
  763. BoxDecoration _cardDecoration(TDThemeData tdTheme) => BoxDecoration(
  764. color: tdTheme.bgColorContainer,
  765. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  766. border: Border.all(color: tdTheme.componentStrokeColor),
  767. );
  768. // ── 申请人 ──
  769. Widget _buildEmployeeCard(AppColorsExtension colors) {
  770. final employees = widget.employees;
  771. return _pickerCard(
  772. label: _l10n.get('applicant'),
  773. required: false,
  774. currentLabel: _selEmployee != null
  775. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  776. : _l10n.get('pleaseSelect'),
  777. labels: employees.map((e) => '${e.salNo}/${e.name}').toList(),
  778. colors: colors,
  779. onSelected: (idx) => setState(() {
  780. _selEmployee = employees[idx];
  781. _bankNameCtrl.text = _selEmployee!.bnkNo;
  782. _bankAccountNameCtrl.text = _selEmployee!.accName;
  783. _bankAccountCtrl.text = _selEmployee!.bnkId;
  784. }),
  785. onClear: _selEmployee != null
  786. ? () => setState(() {
  787. _selEmployee = null;
  788. _bankNameCtrl.clear();
  789. _bankAccountNameCtrl.clear();
  790. _bankAccountCtrl.clear();
  791. })
  792. : null,
  793. );
  794. }
  795. // ── 客户/厂商 ──
  796. Widget _buildCustomerCard(AppColorsExtension colors) {
  797. final vendors = widget.customers;
  798. return _pickerCard(
  799. label: _l10n.get('customerVendor'),
  800. required: false,
  801. currentLabel: _selCustomer != null
  802. ? '${_selCustomer!.id}/${_selCustomer!.name}'
  803. : _l10n.get('pleaseSelect'),
  804. labels: vendors.map((v) => '${v.id}/${v.name}').toList(),
  805. colors: colors,
  806. onSelected: (idx) => setState(() => _selCustomer = vendors[idx]),
  807. onClear: _selCustomer != null
  808. ? () => setState(() => _selCustomer = null)
  809. : null,
  810. );
  811. }
  812. // ── 已充金额 ──
  813. Widget _buildApprovedAmountCard() {
  814. return _inputCard(
  815. label: _l10n.get('approvedAmount'),
  816. required: false,
  817. controller: _approvedAmountCtrl,
  818. hintText: '0',
  819. colors: Theme.of(context).extension<AppColorsExtension>()!,
  820. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  821. inputFormatters: [
  822. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  823. ],
  824. );
  825. }
  826. // ── 备注 ──
  827. Widget _buildRemarkInput(AppColorsExtension colors) {
  828. final tdTheme = TDTheme.of(context);
  829. return TDTextarea(
  830. controller: _remarkCtrl,
  831. focusNode: _remarkFocus,
  832. label: _l10n.get('remark'),
  833. hintText: _l10n.get('enterRemark'),
  834. maxLines: 3,
  835. minLines: 1,
  836. maxLength: 500,
  837. indicator: true,
  838. decoration: BoxDecoration(
  839. color: tdTheme.bgColorContainer,
  840. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  841. border: Border.all(color: tdTheme.componentStrokeColor),
  842. ),
  843. onChanged: (_) => setState(() {}),
  844. );
  845. }
  846. // ── 操作按钮 ──
  847. Widget _buildAttachmentCard(AppColorsExtension colors) {
  848. final tdTheme = TDTheme.of(context);
  849. return Column(
  850. crossAxisAlignment: CrossAxisAlignment.start,
  851. children: [
  852. Padding(
  853. padding: const EdgeInsets.only(left: 4),
  854. child: TDText(
  855. _l10n.get('attachmentUpload'),
  856. font: tdTheme.fontBodyLarge,
  857. fontWeight: FontWeight.w400,
  858. style: const TextStyle(letterSpacing: 0),
  859. ),
  860. ),
  861. Padding(
  862. padding: const EdgeInsets.only(left: 4, top: 4),
  863. child: TDText(
  864. _l10n.get('maxAttachment'),
  865. font: tdTheme.fontBodySmall,
  866. fontWeight: FontWeight.w400,
  867. textColor: tdTheme.textColorPlaceholder,
  868. ),
  869. ),
  870. const SizedBox(height: 4),
  871. if (!_attachAvailable)
  872. TDText(
  873. _l10n.get('attachServiceUnavailable'),
  874. font: tdTheme.fontBodyMedium,
  875. fontWeight: FontWeight.w400,
  876. textColor: tdTheme.textColorPlaceholder,
  877. )
  878. else
  879. AttachmentPicker(
  880. controller: _attachmentCtrl,
  881. maxImageSizeMB: 10,
  882. maxFileSizeMB: 20,
  883. allowedExtensions: const [
  884. 'pdf',
  885. 'doc',
  886. 'docx',
  887. 'xls',
  888. 'xlsx',
  889. 'ppt',
  890. 'pptx',
  891. 'txt',
  892. ],
  893. onFileRejected: (file, reason) {
  894. if (context.mounted) TDToast.showText(reason, context: context);
  895. },
  896. ),
  897. ],
  898. );
  899. }
  900. // ── 会计科目(只读,选择类别后自动带出) ──
  901. Widget _buildAcctSubjectCard(AppColorsExtension colors) {
  902. return _readOnlyCard(
  903. label: _l10n.get('acctSubject'),
  904. value: _selCat.acctSubjectId.isNotEmpty
  905. ? '${_selCat.acctSubjectId}/${_selCat.acctSubjectName}'
  906. : '',
  907. colors: colors,
  908. );
  909. }
  910. Widget _readOnlyCard({
  911. required String label,
  912. required String value,
  913. required AppColorsExtension colors,
  914. }) {
  915. final tdTheme = TDTheme.of(context);
  916. return Container(
  917. padding: const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 12),
  918. decoration: BoxDecoration(
  919. color: tdTheme.bgColorContainer,
  920. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  921. border: Border.all(color: tdTheme.componentStrokeColor),
  922. ),
  923. child: Row(
  924. children: [
  925. TDText(
  926. label,
  927. maxLines: 1,
  928. overflow: TextOverflow.visible,
  929. font: tdTheme.fontBodyLarge,
  930. fontWeight: FontWeight.w400,
  931. style: const TextStyle(letterSpacing: 0),
  932. ),
  933. const SizedBox(width: 12),
  934. Expanded(
  935. child: TDText(
  936. value,
  937. maxLines: 1,
  938. overflow: TextOverflow.ellipsis,
  939. font: tdTheme.fontBodyLarge,
  940. fontWeight: FontWeight.w400,
  941. textColor: tdTheme.textColorPrimary,
  942. textAlign: TextAlign.end,
  943. ),
  944. ),
  945. ],
  946. ),
  947. );
  948. }
  949. // ── 关联项目 ──
  950. Widget _buildProjectCard(AppColorsExtension colors) {
  951. final projects = widget.projects;
  952. return _pickerCard(
  953. label: _l10n.get('relatedProject'),
  954. required: false,
  955. currentLabel: _selProject != null
  956. ? '${_selProject!.id}/${_selProject!.name}'
  957. : _l10n.get('pleaseSelect'),
  958. labels: projects.map((p) => '${p.id}/${p.name}').toList(),
  959. colors: colors,
  960. onSelected: (idx) => setState(() => _selProject = projects[idx]),
  961. onClear: _selProject != null
  962. ? () => setState(() => _selProject = null)
  963. : null,
  964. );
  965. }
  966. // ── 费用承担部门 ──
  967. Widget _buildCostDeptCard(AppColorsExtension colors) {
  968. final depts = widget.costDepts;
  969. return _pickerCard(
  970. label: _l10n.get('costDept'),
  971. required: false,
  972. currentLabel: _selDept != null
  973. ? '${_selDept!.id}/${_selDept!.name}'
  974. : _l10n.get('pleaseSelect'),
  975. labels: depts.map((d) => '${d.id}/${d.name}').toList(),
  976. colors: colors,
  977. onSelected: (idx) => setState(() => _selDept = depts[idx]),
  978. onClear: _selDept != null ? () => setState(() => _selDept = null) : null,
  979. );
  980. }
  981. // ── 收款银行信息 ──
  982. Widget _buildBankInfoCard(AppColorsExtension colors) {
  983. return Column(
  984. crossAxisAlignment: CrossAxisAlignment.start,
  985. children: [
  986. _inputCard(
  987. label: _l10n.get('bankName'),
  988. required: false,
  989. controller: _bankNameCtrl,
  990. hintText: _l10n.get('pleaseEnter'),
  991. colors: colors,
  992. focusNode: _bankNameFocus,
  993. ),
  994. const SizedBox(height: 12),
  995. _inputCard(
  996. label: _l10n.get('bankAccountName'),
  997. required: false,
  998. controller: _bankAccountNameCtrl,
  999. hintText: _l10n.get('pleaseEnter'),
  1000. colors: colors,
  1001. focusNode: _bankAccountNameFocus,
  1002. ),
  1003. const SizedBox(height: 12),
  1004. _inputCard(
  1005. label: _l10n.get('bankAccount'),
  1006. required: false,
  1007. controller: _bankAccountCtrl,
  1008. hintText: _l10n.get('pleaseEnter'),
  1009. colors: colors,
  1010. focusNode: _bankAccountFocus,
  1011. ),
  1012. ],
  1013. );
  1014. }
  1015. Widget _buildActions() {
  1016. return Row(
  1017. children: [
  1018. Expanded(
  1019. child: TDButton(
  1020. text: _l10n.get('cancel'),
  1021. size: TDButtonSize.large,
  1022. type: TDButtonType.outline,
  1023. shape: TDButtonShape.rectangle,
  1024. theme: TDButtonTheme.defaultTheme,
  1025. onTap: () => Navigator.pop(context),
  1026. ),
  1027. ),
  1028. const SizedBox(width: 12),
  1029. Expanded(
  1030. child: TDButton(
  1031. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  1032. size: TDButtonSize.large,
  1033. type: TDButtonType.fill,
  1034. shape: TDButtonShape.rectangle,
  1035. theme: TDButtonTheme.primary,
  1036. onTap: _confirm,
  1037. ),
  1038. ),
  1039. ],
  1040. );
  1041. }
  1042. }