bill_status_bar.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. const BillStatusBar({
  40. this.billStatus,
  41. this.onEdit,
  42. this.onSubmit,
  43. this.onCancelSubmit,
  44. this.onReturn,
  45. this.onTapStatusTag,
  46. this.closedText,
  47. this.transferredText,
  48. this.returnedText,
  49. });
  50. // ── 派生状态 ──
  51. bool get _isClosed => billStatus?['isClosed'] == true;
  52. bool get _isTransferred => billStatus?['isTransferred'] == true;
  53. bool get _isReturned {
  54. final extras = billStatus?['extras'] as Map<String, dynamic>?;
  55. return extras?['isReturned'] == true;
  56. }
  57. String? get _approvalStatus => billStatus?['approvalStatus'] as String?;
  58. String? get _approvalText => billStatus?['approvalText'] as String?;
  59. bool get _canEdit => billStatus?['canEdit'] == true;
  60. // ── 按钮显隐 ──
  61. bool get showEdit {
  62. final s = _approvalStatus;
  63. if (s == 'SHCOUNT0' || s == 'SHCOUNT3') return _canEdit;
  64. if (s == 'SHCOUNT6') return _canEdit;
  65. return false;
  66. }
  67. bool get showSubmit {
  68. final s = _approvalStatus;
  69. return s == 'SHCOUNT0' || s == 'SHCOUNT3';
  70. }
  71. bool get showCancelSubmit => _approvalStatus == 'SHCOUNT1';
  72. bool get showReturn {
  73. if (onReturn == null) return false;
  74. if (_isReturned) return false;
  75. final s = _approvalStatus;
  76. return (s == 'SHCOUNT4' || s == 'SHCOUNT6') &&
  77. !_isClosed &&
  78. !_isTransferred;
  79. }
  80. bool get _showButtons =>
  81. showEdit || showSubmit || showCancelSubmit || showReturn;
  82. // ── 状态标签数据 ──
  83. ({IconData icon, Color color}) _statusStyle() {
  84. if (_isReturned) {
  85. return (icon: Icons.task_alt, color: Colors.deepPurple);
  86. }
  87. if (_isClosed) {
  88. return (icon: Icons.lock_outline, color: Colors.blueGrey);
  89. }
  90. if (_isTransferred) {
  91. return (icon: Icons.swap_horiz, color: Colors.blue);
  92. }
  93. final s = _approvalStatus;
  94. if (s == null || s.isEmpty) {
  95. return (icon: Icons.help_outline, color: Colors.grey);
  96. }
  97. switch (s) {
  98. case 'SHCOUNT0':
  99. return (icon: Icons.edit_note, color: Colors.teal);
  100. case 'SHCOUNT1':
  101. return (icon: Icons.hourglass_empty, color: Colors.orange);
  102. case 'SHCOUNT2':
  103. return (icon: Icons.cancel_outlined, color: Colors.red);
  104. case 'SHCOUNT3':
  105. return (icon: Icons.undo, color: Colors.deepOrange);
  106. case 'SHCOUNT4':
  107. return (icon: Icons.check_circle_outline, color: Colors.green);
  108. case 'SHCOUNT5':
  109. return (icon: Icons.sync, color: Colors.blue);
  110. case 'SHCOUNT6':
  111. return (icon: Icons.verified, color: Colors.indigo);
  112. default:
  113. return (icon: Icons.help_outline, color: Colors.grey);
  114. }
  115. }
  116. String? get _displayText {
  117. if (_isReturned) return returnedText;
  118. if (_isClosed) return closedText;
  119. if (_isTransferred) return transferredText;
  120. return _approvalText;
  121. }
  122. // ── 构建 ──
  123. /// 仅状态标签(用于 FormSection trailing)
  124. Widget buildStatusTag(BuildContext context) {
  125. final style = _statusStyle();
  126. final text = _displayText;
  127. if (text == null || text.isEmpty) return const SizedBox.shrink();
  128. final tag = Container(
  129. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
  130. decoration: BoxDecoration(
  131. color: style.color.withValues(alpha: 0.1),
  132. borderRadius: BorderRadius.circular(6),
  133. border: Border.all(
  134. color: style.color.withValues(alpha: 0.3),
  135. width: 0.5,
  136. ),
  137. ),
  138. child: Row(
  139. mainAxisSize: MainAxisSize.min,
  140. children: [
  141. Icon(style.icon, size: 18, color: style.color),
  142. const SizedBox(width: 4),
  143. Text(
  144. text,
  145. style: TextStyle(
  146. fontSize: 13,
  147. fontWeight: FontWeight.w600,
  148. color: style.color,
  149. ),
  150. ),
  151. ],
  152. ),
  153. );
  154. if (onTapStatusTag != null) {
  155. return GestureDetector(onTap: onTapStatusTag, child: tag);
  156. }
  157. return tag;
  158. }
  159. /// 底部操作按钮区
  160. Widget buildActions(BuildContext context) {
  161. if (!_showButtons) return const SizedBox.shrink();
  162. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  163. final l10n = AppLocalizations.of(context);
  164. final bottomPadding = MediaQuery.of(context).padding.bottom;
  165. return Container(
  166. padding: EdgeInsets.fromLTRB(16, 12, 16, 12 + bottomPadding),
  167. decoration: BoxDecoration(
  168. color: colors.bgCard,
  169. border: Border(top: BorderSide(color: colors.border)),
  170. ),
  171. child: Row(
  172. children: [
  173. if (showEdit) ...[
  174. Expanded(
  175. child: TDButton(
  176. icon: TDIcons.edit,
  177. text: _editLabel(l10n),
  178. size: TDButtonSize.medium,
  179. type: TDButtonType.outline,
  180. shape: TDButtonShape.round,
  181. theme: TDButtonTheme.primary,
  182. onTap: onEdit,
  183. ),
  184. ),
  185. ],
  186. if (showEdit && (showSubmit || showCancelSubmit))
  187. const SizedBox(width: 12),
  188. if (showSubmit)
  189. Expanded(
  190. child: TDButton(
  191. icon: TDIcons.send,
  192. text: _submitLabel(l10n),
  193. size: TDButtonSize.medium,
  194. type: TDButtonType.fill,
  195. shape: TDButtonShape.round,
  196. theme: TDButtonTheme.primary,
  197. onTap: onSubmit,
  198. ),
  199. ),
  200. if (showCancelSubmit)
  201. Expanded(
  202. child: TDButton(
  203. icon: TDIcons.rollback,
  204. text: _cancelSubmitLabel(l10n),
  205. size: TDButtonSize.medium,
  206. type: TDButtonType.fill,
  207. shape: TDButtonShape.round,
  208. theme: TDButtonTheme.danger,
  209. onTap: onCancelSubmit,
  210. ),
  211. ),
  212. if (showReturn) ...[
  213. if (showEdit || showSubmit || showCancelSubmit)
  214. const SizedBox(width: 12),
  215. Expanded(
  216. child: TDButton(
  217. icon: Icons.directions_car,
  218. text: _returnLabel(l10n),
  219. size: TDButtonSize.medium,
  220. type: TDButtonType.fill,
  221. shape: TDButtonShape.round,
  222. theme: TDButtonTheme.primary,
  223. onTap: onReturn,
  224. ),
  225. ),
  226. ],
  227. ],
  228. ),
  229. );
  230. }
  231. // ── 按钮标签 ──
  232. /// 默认返回中文按钮标签(不依赖 i18n,简化跨模块复用)
  233. String _editLabel(AppLocalizations l10n) => l10n.get('modify');
  234. String _submitLabel(AppLocalizations l10n) => l10n.get('submit');
  235. String _cancelSubmitLabel(AppLocalizations l10n) => l10n.get('cancelSubmit');
  236. String _returnLabel(AppLocalizations l10n) => l10n.get('returnCar');
  237. }