| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:tdesign_flutter/tdesign_flutter.dart';
- import '../../../core/i18n/app_localizations.dart';
- import '../../../core/theme/app_colors.dart';
- import '../../../core/theme/app_colors_extension.dart';
- import '../../../core/data/mock_api_data.dart';
- /// 报销明细输入数据。
- class ExpenseDetailInputData {
- final String category;
- final String categoryName;
- final String purpose;
- final double amount; // 含税金额
- final double taxRate;
- final String customerVendorName;
- final double offsetAmount;
- final String remark;
- const ExpenseDetailInputData({
- required this.category,
- required this.categoryName,
- required this.purpose,
- required this.amount,
- required this.taxRate,
- this.customerVendorName = '',
- this.offsetAmount = 0.0,
- this.remark = '',
- });
- }
- /// 报销明细编辑弹窗。
- ///
- /// 使用 [TDSlidePopupRoute] 从底部滑出,卡片化展示表单字段。
- /// 参照 ExpenseApplyCreatePage 的 ExpenseDetailDialog 样式。
- class ExpenseDetailDialog extends StatefulWidget {
- final List<CostCategory> categories;
- final AppLocalizations l10n;
- const ExpenseDetailDialog({
- super.key,
- required this.categories,
- required this.l10n,
- });
- /// 显示弹窗,返回 [ExpenseDetailInputData] 或 `null`(取消时)。
- static Future<ExpenseDetailInputData?> show(
- BuildContext context, {
- required List<CostCategory> categories,
- required AppLocalizations l10n,
- }) {
- FocusScope.of(context).unfocus();
- return Navigator.push<ExpenseDetailInputData>(
- context,
- TDSlidePopupRoute<ExpenseDetailInputData>(
- slideTransitionFrom: SlideTransitionFrom.bottom,
- isDismissible: false,
- builder: (_) => ExpenseDetailDialog(
- categories: categories,
- l10n: l10n,
- ),
- ),
- );
- }
- @override
- State<ExpenseDetailDialog> createState() =>
- _ExpenseDetailDialogState();
- }
- class _ExpenseDetailDialogState extends State<ExpenseDetailDialog> {
- late String _cat;
- late String _catLabel;
- late TextEditingController _descCtrl;
- late TextEditingController _amountCtrl;
- late TextEditingController _customerCtrl;
- late TextEditingController _offsetCtrl;
- late TextEditingController _remarkCtrl;
- double _taxRate = 0.06;
- static const _taxOptions = [0.06, 0.09, 0.13];
- static const _taxLabels = ['6%', '9%', '13%'];
- List<CostCategory> get _cats => widget.categories;
- AppLocalizations get _l10n => widget.l10n;
- @override
- void initState() {
- super.initState();
- _cat = _cats.isNotEmpty ? _cats.first.code : 'other';
- _catLabel = _l10n.get(_cats.firstWhere((c) => c.code == _cat).nameKey);
- _descCtrl = TextEditingController();
- _amountCtrl = TextEditingController();
- _customerCtrl = TextEditingController();
- _offsetCtrl = TextEditingController();
- _remarkCtrl = TextEditingController();
- }
- @override
- void dispose() {
- _descCtrl.dispose();
- _amountCtrl.dispose();
- _customerCtrl.dispose();
- _offsetCtrl.dispose();
- _remarkCtrl.dispose();
- super.dispose();
- }
- void _confirm() {
- final amount = double.tryParse(_amountCtrl.text) ?? 0;
- final desc = _descCtrl.text.trim();
- if (desc.isEmpty) {
- TDToast.showText(_l10n.get('enterExpenseName'), context: context);
- return;
- }
- if (amount <= 0) {
- TDToast.showText(_l10n.get('amountPositive'), context: context);
- return;
- }
- Navigator.pop(
- context,
- ExpenseDetailInputData(
- category: _cat,
- categoryName: _l10n.get(
- _cats.firstWhere((c) => c.code == _cat).nameKey,
- ),
- purpose: desc,
- amount: amount,
- taxRate: _taxRate,
- customerVendorName: _customerCtrl.text.trim(),
- offsetAmount: double.tryParse(_offsetCtrl.text) ?? 0,
- remark: _remarkCtrl.text.trim(),
- ),
- );
- }
- double get _amountExclTax => _taxRate > 0
- ? (double.tryParse(_amountCtrl.text) ?? 0) / (1 + _taxRate)
- : (double.tryParse(_amountCtrl.text) ?? 0);
- double get _taxAmount =>
- (double.tryParse(_amountCtrl.text) ?? 0) - _amountExclTax;
- @override
- Widget build(BuildContext context) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final bottomInset = MediaQuery.of(context).viewInsets.bottom;
- return SafeArea(
- child: Padding(
- padding: EdgeInsets.only(bottom: bottomInset),
- child: Container(
- decoration: BoxDecoration(
- color: colors.bgPage,
- borderRadius:
- const BorderRadius.vertical(top: Radius.circular(16)),
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- _buildHeader(colors),
- Flexible(
- child: SingleChildScrollView(
- padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- _buildCategoryCard(colors),
- const SizedBox(height: 12),
- _buildDescCard(),
- const SizedBox(height: 12),
- _buildAmountCard(),
- const SizedBox(height: 12),
- _buildTaxRateCard(colors),
- const SizedBox(height: 12),
- // 自动计算展示
- _buildCalcInfo(colors),
- const SizedBox(height: 12),
- _buildCustomerCard(),
- const SizedBox(height: 12),
- _buildOffsetCard(),
- const SizedBox(height: 12),
- _buildRemarkCard(colors),
- ],
- ),
- ),
- ),
- Container(
- padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
- decoration: BoxDecoration(
- color: colors.bgCard,
- border: Border(
- top: BorderSide(color: colors.border, width: 0.5),
- ),
- ),
- child: _buildActions(),
- ),
- ],
- ),
- ),
- ),
- );
- }
- // ── 标题栏 ──
- Widget _buildHeader(AppColorsExtension colors) {
- return Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Center(
- child: Container(
- margin: const EdgeInsets.only(top: 8, bottom: 4),
- width: 36,
- height: 4,
- decoration: BoxDecoration(
- color: colors.border,
- borderRadius: BorderRadius.circular(2),
- ),
- ),
- ),
- Padding(
- padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
- child: Row(
- children: [
- const SizedBox(width: 28),
- Expanded(
- child: Center(
- child: Text(
- _l10n.get('addExpenseDetail'),
- style: TextStyle(
- fontSize: AppFontSizes.title,
- fontWeight: FontWeight.w600,
- color: colors.textPrimary,
- ),
- ),
- ),
- ),
- GestureDetector(
- onTap: () => Navigator.pop(context),
- child: Padding(
- padding: const EdgeInsets.all(4),
- child: Icon(
- Icons.close,
- size: 20,
- color: colors.textSecondary,
- ),
- ),
- ),
- ],
- ),
- ),
- ],
- );
- }
- // ── picker 卡片 ──
- Widget _pickerCard({
- required String label,
- required bool required,
- required String currentLabel,
- required List<String> labels,
- required ValueChanged<int> onSelected,
- required AppColorsExtension colors,
- }) {
- final tdTheme = TDTheme.of(context);
- return GestureDetector(
- onTap: () {
- TDPicker.showMultiPicker(
- context,
- title: label,
- backgroundColor: colors.bgCard,
- data: [labels],
- onConfirm: (selected) {
- if (selected.isNotEmpty && selected[0] is int) {
- final idx = selected[0] as int;
- if (idx >= 0 && idx < labels.length) {
- Navigator.of(context).pop();
- onSelected(idx);
- }
- }
- },
- );
- },
- child: Container(
- padding: const EdgeInsets.only(
- left: 16, right: 16, top: 12, bottom: 12,
- ),
- decoration: BoxDecoration(
- color: tdTheme.bgColorContainer,
- borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
- border: Border.all(color: tdTheme.componentStrokeColor),
- ),
- child: Row(
- children: [
- TDText(
- label,
- maxLines: 1,
- overflow: TextOverflow.visible,
- font: tdTheme.fontBodyLarge,
- fontWeight: FontWeight.w400,
- style: const TextStyle(letterSpacing: 0),
- ),
- if (required)
- Padding(
- padding: const EdgeInsets.only(left: 4),
- child: TDText(
- '*',
- font: tdTheme.fontBodyLarge,
- fontWeight: FontWeight.w400,
- style: TextStyle(color: tdTheme.errorColor6),
- ),
- ),
- const SizedBox(width: 12),
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- mainAxisSize: MainAxisSize.max,
- children: [
- Flexible(
- child: TDText(
- currentLabel,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- font: tdTheme.fontBodyLarge,
- fontWeight: FontWeight.w400,
- textColor: tdTheme.textColorPrimary,
- textAlign: TextAlign.end,
- ),
- ),
- const SizedBox(width: 4),
- Icon(
- Icons.chevron_right,
- size: 18,
- color: tdTheme.textColorPlaceholder,
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- );
- }
- // ── 费用类别 ──
- Widget _buildCategoryCard(AppColorsExtension colors) {
- return _pickerCard(
- label: _l10n.get('expenseCategory'),
- required: true,
- currentLabel: _catLabel,
- labels: _cats.map((c) => _l10n.get(c.nameKey)).toList(),
- colors: colors,
- onSelected: (idx) => setState(() {
- _cat = _cats[idx].code;
- _catLabel = _l10n.get(_cats[idx].nameKey);
- }),
- );
- }
- // ── 费用项目 ──
- Widget _buildDescCard() {
- final screenWidth = MediaQuery.of(context).size.width;
- return TDInput(
- type: TDInputType.cardStyle,
- cardStyle: TDCardStyle.topText,
- width: screenWidth - 32,
- leftLabel: _l10n.get('expenseName'),
- required: true,
- controller: _descCtrl,
- hintText: _l10n.get('enterExpenseName'),
- contentAlignment: TextAlign.center,
- showBottomDivider: false,
- onChanged: (_) => setState(() {}),
- onClearTap: () {
- _descCtrl.clear();
- setState(() {});
- },
- );
- }
- // ── 金额(含税) ──
- Widget _buildAmountCard() {
- final screenWidth = MediaQuery.of(context).size.width;
- return TDInput(
- type: TDInputType.cardStyle,
- cardStyle: TDCardStyle.topText,
- width: screenWidth - 32,
- leftLabel: _l10n.get('amountInclTax'),
- required: true,
- controller: _amountCtrl,
- hintText: '>0',
- contentAlignment: TextAlign.center,
- inputType: const TextInputType.numberWithOptions(decimal: true),
- inputFormatters: [
- FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
- ],
- showBottomDivider: false,
- onChanged: (_) => setState(() {}),
- onClearTap: () {
- _amountCtrl.clear();
- setState(() {});
- },
- );
- }
- // ── 税率 ──
- Widget _buildTaxRateCard(AppColorsExtension colors) {
- final currentLabel =
- '${(_taxRate * 100).toStringAsFixed(0)}%';
- return _pickerCard(
- label: _l10n.get('taxRate'),
- required: true,
- currentLabel: currentLabel,
- labels: _taxLabels.toList(),
- colors: colors,
- onSelected: (idx) => setState(() {
- _taxRate = _taxOptions[idx];
- }),
- );
- }
- // ── 计算信息 ──
- Widget _buildCalcInfo(AppColorsExtension colors) {
- final amount = double.tryParse(_amountCtrl.text) ?? 0;
- if (amount <= 0) return const SizedBox.shrink();
- return Container(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
- decoration: BoxDecoration(
- color: colors.primary50,
- borderRadius: BorderRadius.circular(8),
- ),
- child: Row(
- children: [
- Expanded(
- child: Text(
- '${_l10n.get('amountExcludingTax')}: ¥${_amountExclTax.toStringAsFixed(2)}',
- style: TextStyle(
- fontSize: AppFontSizes.body,
- color: colors.textSecondary,
- ),
- ),
- ),
- Text(
- '${_l10n.get('taxAmount')}: ¥${_taxAmount.toStringAsFixed(2)}',
- style: TextStyle(
- fontSize: AppFontSizes.body,
- color: colors.textSecondary,
- ),
- ),
- ],
- ),
- );
- }
- // ── 客户/厂商 ──
- Widget _buildCustomerCard() {
- final screenWidth = MediaQuery.of(context).size.width;
- return TDInput(
- type: TDInputType.cardStyle,
- cardStyle: TDCardStyle.topText,
- width: screenWidth - 32,
- leftLabel: _l10n.get('customerVendor'),
- controller: _customerCtrl,
- hintText: _l10n.get('optional'),
- contentAlignment: TextAlign.center,
- showBottomDivider: false,
- onChanged: (_) => setState(() {}),
- onClearTap: () {
- _customerCtrl.clear();
- setState(() {});
- },
- );
- }
- // ── 已充金额 ──
- Widget _buildOffsetCard() {
- final screenWidth = MediaQuery.of(context).size.width;
- return TDInput(
- type: TDInputType.cardStyle,
- cardStyle: TDCardStyle.topText,
- width: screenWidth - 32,
- leftLabel: _l10n.get('offsetAmount'),
- controller: _offsetCtrl,
- hintText: '0',
- contentAlignment: TextAlign.center,
- inputType: const TextInputType.numberWithOptions(decimal: true),
- inputFormatters: [
- FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
- ],
- showBottomDivider: false,
- onChanged: (_) => setState(() {}),
- onClearTap: () {
- _offsetCtrl.clear();
- setState(() {});
- },
- );
- }
- // ── 备注 ──
- Widget _buildRemarkCard(AppColorsExtension colors) {
- final tdTheme = TDTheme.of(context);
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.only(left: 4),
- child: TDText(
- _l10n.get('detailRemark'),
- font: tdTheme.fontBodyLarge,
- fontWeight: FontWeight.w400,
- style: const TextStyle(letterSpacing: 0),
- ),
- ),
- const SizedBox(height: 8),
- TDTextarea(
- controller: _remarkCtrl,
- hintText: _l10n.get('optional'),
- maxLines: 3,
- minLines: 1,
- maxLength: 200,
- indicator: true,
- padding: EdgeInsets.zero,
- bordered: true,
- inputType: TextInputType.multiline,
- backgroundColor: tdTheme.bgColorContainer,
- ),
- ],
- );
- }
- // ── 操作按钮 ──
- Widget _buildActions() {
- return Row(
- children: [
- Expanded(
- child: TDButton(
- text: _l10n.get('cancel'),
- size: TDButtonSize.large,
- type: TDButtonType.outline,
- shape: TDButtonShape.rectangle,
- theme: TDButtonTheme.defaultTheme,
- onTap: () => Navigator.pop(context),
- ),
- ),
- const SizedBox(width: 12),
- Expanded(
- child: TDButton(
- text: _l10n.get('confirmAdd'),
- size: TDButtonSize.large,
- type: TDButtonType.fill,
- shape: TDButtonShape.rectangle,
- theme: TDButtonTheme.primary,
- onTap: _confirm,
- ),
- ),
- ],
- );
- }
- }
|