bill_status_bar.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import 'package:flutter/material.dart';
  2. import 'package:tdesign_flutter/tdesign_flutter.dart';
  3. import '../../core/i18n/app_localizations.dart';
  4. import '../../core/theme/app_colors_extension.dart';
  5. /// 单据状态标签 + 底部操作按钮。
  6. ///
  7. /// 根据 [billStatus] 中的 `approvalStatus`(SHCOUNT0~6)、
  8. /// `isClosed`、`isTransferred`、`isCreator`、`canEdit` 控制展示。
  9. ///
  10. /// 使用方式:
  11. /// ```dart
  12. /// BillStatusBar(
  13. /// billStatus: _billStatus,
  14. /// onEdit: () => context.push('/xxx/edit/$billNo'),
  15. /// onSubmit: () => api.shSubmit(bilNo: billNo, bilDd: ...),
  16. /// onCancelSubmit: () => api.shSubmit(bilNo: billNo, bilDd: ..., isCancel: true),
  17. /// onTapStatusTag: () => _showAuditTrail(),
  18. /// ),
  19. /// ```
  20. class BillStatusBar {
  21. /// getBillStatus 接口返回的完整 Map,为 null 时显示空状态。
  22. final Map<String, dynamic>? billStatus;
  23. /// "修改"按钮回调
  24. final VoidCallback? onEdit;
  25. /// "提交"按钮回调(shSubmit)
  26. final VoidCallback? onSubmit;
  27. /// "取消提交"按钮回调(shSubmit + isCancel)
  28. final VoidCallback? onCancelSubmit;
  29. /// "还车"按钮回调(仅用车申请单使用)
  30. final VoidCallback? onReturn;
  31. /// 点击状态标签回调(查看审批轨迹)
  32. final VoidCallback? onTapStatusTag;
  33. /// 已关闭状态文本(如"已关闭",仅 expense 使用)
  34. final String? closedText;
  35. /// 已转单状态文本(如"已转为报销单",expense/expense_apply 使用)
  36. final String? transferredText;
  37. /// 已还车状态文本(如"已还车",仅用车申请单使用,优先级最高)
  38. final String? returnedText;
  39. /// 是否允许手动提交审核流。为 false 时提交/取消提交按钮不显示。
  40. /// 当 ERP 已自动处理存盘立即提交时,无需在详情页再次手动操作。
  41. final bool canManualSubmit;
  42. const BillStatusBar({
  43. this.billStatus,
  44. this.onEdit,
  45. this.onSubmit,
  46. this.onCancelSubmit,
  47. this.onReturn,
  48. this.onTapStatusTag,
  49. this.closedText,
  50. this.transferredText,
  51. this.returnedText,
  52. this.canManualSubmit = true,
  53. });
  54. // ── 派生状态 ──
  55. bool get _isClosed => billStatus?['isClosed'] == true;
  56. bool get _isTransferred => billStatus?['isTransferred'] == true;
  57. bool get _isReturned {
  58. final extras = billStatus?['extras'] as Map<String, dynamic>?;
  59. return extras?['isReturned'] == true;
  60. }
  61. String? get _approvalStatus => billStatus?['approvalStatus'] as String?;
  62. String? get _approvalText => billStatus?['approvalText'] as String?;
  63. bool get _canEdit => billStatus?['canEdit'] == true;
  64. // ── 按钮显隐 ──
  65. bool get showEdit {
  66. final s = _approvalStatus;
  67. if (s == 'SHCOUNT0' || s == 'SHCOUNT3') return _canEdit;
  68. if (s == 'SHCOUNT6') return _canEdit;
  69. return false;
  70. }
  71. bool get showSubmit {
  72. if (!canManualSubmit) return false;
  73. if (_isClosed || _isTransferred) return false;
  74. final s = _approvalStatus;
  75. return s == 'SHCOUNT0' || s == 'SHCOUNT3';
  76. }
  77. bool get showCancelSubmit {
  78. if (!canManualSubmit) return false;
  79. if (_isClosed || _isTransferred) return false;
  80. return _approvalStatus == 'SHCOUNT1';
  81. }
  82. bool get showReturn {
  83. if (onReturn == null) return false;
  84. if (_isReturned) return false;
  85. final s = _approvalStatus;
  86. return (s == 'SHCOUNT4' || s == 'SHCOUNT6') &&
  87. !_isClosed &&
  88. !_isTransferred;
  89. }
  90. bool get _showButtons =>
  91. showEdit || showSubmit || showCancelSubmit || showReturn;
  92. // ── 状态标签数据 ──
  93. ({IconData icon, Color color}) _statusStyle() {
  94. if (_isReturned) {
  95. return (icon: Icons.task_alt, color: Colors.deepPurple);
  96. }
  97. if (_isClosed) {
  98. return (icon: Icons.lock_outline, color: Colors.blueGrey);
  99. }
  100. if (_isTransferred) {
  101. return (icon: Icons.swap_horiz, color: Colors.blue);
  102. }
  103. final s = _approvalStatus;
  104. if (s == null || s.isEmpty) {
  105. return (icon: Icons.help_outline, color: Colors.grey);
  106. }
  107. switch (s) {
  108. case 'SHCOUNT0':
  109. return (icon: Icons.edit_note, color: Colors.teal);
  110. case 'SHCOUNT1':
  111. return (icon: Icons.hourglass_empty, color: Colors.orange);
  112. case 'SHCOUNT2':
  113. return (icon: Icons.cancel_outlined, color: Colors.red);
  114. case 'SHCOUNT3':
  115. return (icon: Icons.undo, color: Colors.deepOrange);
  116. case 'SHCOUNT4':
  117. return (icon: Icons.check_circle_outline, color: Colors.green);
  118. case 'SHCOUNT5':
  119. return (icon: Icons.sync, color: Colors.blue);
  120. case 'SHCOUNT6':
  121. return (icon: Icons.verified, color: Colors.indigo);
  122. default:
  123. return (icon: Icons.help_outline, color: Colors.grey);
  124. }
  125. }
  126. String? get _displayText {
  127. if (_isReturned) return returnedText;
  128. if (_isClosed) return closedText;
  129. if (_isTransferred) return transferredText;
  130. return _approvalText;
  131. }
  132. // ── 构建 ──
  133. /// 仅状态标签(用于 FormSection trailing)
  134. Widget buildStatusTag(BuildContext context) {
  135. final style = _statusStyle();
  136. final text = _displayText;
  137. if (text == null || text.isEmpty) return const SizedBox.shrink();
  138. final tag = Container(
  139. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
  140. decoration: BoxDecoration(
  141. color: style.color.withValues(alpha: 0.1),
  142. borderRadius: BorderRadius.circular(6),
  143. border: Border.all(
  144. color: style.color.withValues(alpha: 0.3),
  145. width: 0.5,
  146. ),
  147. ),
  148. child: Row(
  149. mainAxisSize: MainAxisSize.min,
  150. children: [
  151. Icon(style.icon, size: 18, color: style.color),
  152. const SizedBox(width: 4),
  153. Text(
  154. text,
  155. style: TextStyle(
  156. fontSize: 13,
  157. fontWeight: FontWeight.w600,
  158. color: style.color,
  159. ),
  160. ),
  161. ],
  162. ),
  163. );
  164. if (onTapStatusTag != null) {
  165. return GestureDetector(onTap: onTapStatusTag, child: tag);
  166. }
  167. return tag;
  168. }
  169. /// 底部操作按钮区
  170. Widget buildActions(BuildContext context) {
  171. if (!_showButtons) return const SizedBox.shrink();
  172. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  173. final l10n = AppLocalizations.of(context);
  174. final bottomPadding = MediaQuery.of(context).padding.bottom;
  175. return Container(
  176. padding: EdgeInsets.fromLTRB(16, 12, 16, 12 + bottomPadding),
  177. decoration: BoxDecoration(
  178. color: colors.bgCard,
  179. border: Border(top: BorderSide(color: colors.border)),
  180. ),
  181. child: Row(
  182. children: [
  183. if (showEdit) ...[
  184. Expanded(
  185. child: TDButton(
  186. icon: TDIcons.edit,
  187. text: _editLabel(l10n),
  188. size: TDButtonSize.medium,
  189. type: TDButtonType.outline,
  190. shape: TDButtonShape.round,
  191. theme: TDButtonTheme.primary,
  192. onTap: onEdit,
  193. ),
  194. ),
  195. ],
  196. if (showEdit && (showSubmit || showCancelSubmit))
  197. const SizedBox(width: 12),
  198. if (showSubmit)
  199. Expanded(
  200. child: TDButton(
  201. icon: TDIcons.send,
  202. text: _submitLabel(l10n),
  203. size: TDButtonSize.medium,
  204. type: TDButtonType.fill,
  205. shape: TDButtonShape.round,
  206. theme: TDButtonTheme.primary,
  207. onTap: onSubmit,
  208. ),
  209. ),
  210. if (showCancelSubmit)
  211. Expanded(
  212. child: TDButton(
  213. icon: TDIcons.rollback,
  214. text: _cancelSubmitLabel(l10n),
  215. size: TDButtonSize.medium,
  216. type: TDButtonType.fill,
  217. shape: TDButtonShape.round,
  218. theme: TDButtonTheme.danger,
  219. onTap: onCancelSubmit,
  220. ),
  221. ),
  222. if (showReturn) ...[
  223. if (showEdit || showSubmit || showCancelSubmit)
  224. const SizedBox(width: 12),
  225. Expanded(
  226. child: TDButton(
  227. icon: Icons.directions_car,
  228. text: _returnLabel(l10n),
  229. size: TDButtonSize.medium,
  230. type: TDButtonType.fill,
  231. shape: TDButtonShape.round,
  232. theme: TDButtonTheme.primary,
  233. onTap: onReturn,
  234. ),
  235. ),
  236. ],
  237. ],
  238. ),
  239. );
  240. }
  241. // ── 按钮标签 ──
  242. /// 默认返回中文按钮标签(不依赖 i18n,简化跨模块复用)
  243. String _editLabel(AppLocalizations l10n) => l10n.get('modify');
  244. String _submitLabel(AppLocalizations l10n) => l10n.get('submit');
  245. String _cancelSubmitLabel(AppLocalizations l10n) => l10n.get('cancelSubmit');
  246. String _returnLabel(AppLocalizations l10n) => l10n.get('returnCar');
  247. }