form_field_row.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:tdesign_flutter/tdesign_flutter.dart';
  4. import '../../core/theme/app_colors.dart';
  5. import '../../core/theme/app_colors_extension.dart';
  6. import '../../core/i18n/app_localizations.dart';
  7. /// 表单字段行,左侧标签 + 右侧值 + 可选箭头。
  8. ///
  9. /// [required] 为 true 时标签前显示红色星号。
  10. class FormFieldRow extends StatelessWidget {
  11. final String label;
  12. final String? value;
  13. final String? hint;
  14. final bool showArrow;
  15. final bool readOnly;
  16. final bool required;
  17. final VoidCallback? onTap;
  18. final VoidCallback? onClear;
  19. final bool bold;
  20. final bool showMoreOnOverflow;
  21. final Color? valueColor;
  22. const FormFieldRow({
  23. super.key,
  24. required this.label,
  25. this.value,
  26. this.hint,
  27. this.showArrow = true,
  28. this.readOnly = false,
  29. this.required = false,
  30. this.onTap,
  31. this.onClear,
  32. this.bold = false,
  33. this.showMoreOnOverflow = true,
  34. this.valueColor,
  35. });
  36. @override
  37. Widget build(BuildContext context) {
  38. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  39. final hasValue = value != null && value!.isNotEmpty;
  40. final hintText =
  41. hint ?? AppLocalizations.of(context).get('pleaseSelectOrFill');
  42. final displayText = hasValue ? value! : hintText;
  43. // 值文本长(≥8 个中文字符宽度)→ 标签最多 1/3
  44. // 值文本短 → 标签最多 2/3
  45. final textScaler = MediaQuery.textScalerOf(context);
  46. final valEstimate = textScaler.scale(displayText.length * 14.0);
  47. final valueNeedsMore = valEstimate > 110;
  48. final labelMaxRatio = valueNeedsMore ? 1 / 3 : 2 / 3;
  49. final labelStyle = TextStyle(
  50. fontSize: AppFontSizes.subtitle,
  51. color: colors.textSecondary,
  52. );
  53. final asteriskStyle = TextStyle(
  54. fontSize: AppFontSizes.subtitle,
  55. color: colors.danger,
  56. );
  57. final valueTextStyle = TextStyle(
  58. fontSize: AppFontSizes.subtitle,
  59. fontWeight: bold ? FontWeight.w600 : FontWeight.normal,
  60. color: hasValue
  61. ? (valueColor ?? colors.textPrimary)
  62. : colors.textPlaceholder,
  63. );
  64. return GestureDetector(
  65. onTap: onTap,
  66. behavior: HitTestBehavior.opaque,
  67. child: Container(
  68. padding: EdgeInsets.zero,
  69. child: Builder(
  70. key: ValueKey(displayText),
  71. builder: (ctx) => LayoutBuilder(
  72. builder: (ctx, outer) {
  73. final labelMaxWidth = (outer.maxWidth - 16) * labelMaxRatio;
  74. return Row(
  75. children: [
  76. // ── 标签:自然宽度,ConstrainedBox 上限 ──
  77. ConstrainedBox(
  78. constraints: BoxConstraints(maxWidth: labelMaxWidth),
  79. child: _LabelContent(
  80. label: label,
  81. required: required,
  82. textScaler: textScaler,
  83. labelStyle: labelStyle,
  84. asteriskStyle: asteriskStyle,
  85. ),
  86. ),
  87. const SizedBox(width: 16),
  88. // ── 值:Expanded 填满剩余空间 → 靠右 ──
  89. Expanded(
  90. child: _ValueContent(
  91. displayText: displayText,
  92. hasValue: hasValue,
  93. readOnly: readOnly,
  94. showArrow: showArrow,
  95. bold: bold,
  96. showMoreOnOverflow: showMoreOnOverflow,
  97. valueColor: valueColor,
  98. onClear: onClear,
  99. onShowFull: (text) => _showFullValue(context, text),
  100. textStyle: valueTextStyle,
  101. ),
  102. ),
  103. ],
  104. );
  105. },
  106. ),
  107. ),
  108. ),
  109. );
  110. }
  111. void _showFullValue(BuildContext context, String text) {
  112. final l10n = AppLocalizations.of(context);
  113. showDialog(
  114. context: context,
  115. builder: (ctx) => TDAlertDialog(
  116. title: label,
  117. content: text,
  118. buttonStyle: TDDialogButtonStyle.text,
  119. rightBtn: TDDialogButtonOptions(
  120. title: l10n.get('close'),
  121. action: () => Navigator.pop(ctx),
  122. ),
  123. leftBtn: TDDialogButtonOptions(
  124. title: l10n.get('copy'),
  125. action: () {
  126. Clipboard.setData(ClipboardData(text: text));
  127. Navigator.pop(ctx);
  128. },
  129. ),
  130. ),
  131. );
  132. }
  133. }
  134. /// 标签内容:文字 + 必填星号 + 溢出 tooltip
  135. class _LabelContent extends StatelessWidget {
  136. final String label;
  137. final bool required;
  138. final TextScaler textScaler;
  139. final TextStyle labelStyle;
  140. final TextStyle asteriskStyle;
  141. const _LabelContent({
  142. required this.label,
  143. required this.required,
  144. required this.textScaler,
  145. required this.labelStyle,
  146. required this.asteriskStyle,
  147. });
  148. @override
  149. Widget build(BuildContext context) {
  150. return LayoutBuilder(
  151. builder: (context, constraints) {
  152. final labelTp = TextPainter(
  153. text: TextSpan(text: label, style: labelStyle),
  154. textDirection: TextDirection.ltr,
  155. textScaler: textScaler,
  156. maxLines: 1,
  157. )..layout();
  158. final astWidth = required
  159. ? (TextPainter(
  160. text: TextSpan(text: ' *', style: asteriskStyle),
  161. textDirection: TextDirection.ltr,
  162. textScaler: textScaler,
  163. maxLines: 1,
  164. )..layout()).width
  165. : 0.0;
  166. final overflows = (labelTp.width + astWidth) > constraints.maxWidth;
  167. return Row(
  168. mainAxisSize: MainAxisSize.min,
  169. children: [
  170. Flexible(
  171. child: Text(
  172. label,
  173. style: labelStyle,
  174. maxLines: 1,
  175. softWrap: false,
  176. overflow: TextOverflow.fade,
  177. ),
  178. ),
  179. if (required) Text(' *', style: asteriskStyle),
  180. if (overflows)
  181. GestureDetector(
  182. onTap: () {},
  183. child: Tooltip(
  184. message: label,
  185. triggerMode: TooltipTriggerMode.tap,
  186. preferBelow: false,
  187. child: Padding(
  188. padding: const EdgeInsets.only(left: 2),
  189. child: Icon(
  190. Icons.info_outline,
  191. size: 14,
  192. color: Colors.grey,
  193. ),
  194. ),
  195. ),
  196. ),
  197. ],
  198. );
  199. },
  200. );
  201. }
  202. }
  203. /// 值内容:文字 + clear / more / chevron 图标
  204. class _ValueContent extends StatelessWidget {
  205. final String displayText;
  206. final bool hasValue;
  207. final bool readOnly;
  208. final bool showArrow;
  209. final bool bold;
  210. final bool showMoreOnOverflow;
  211. final Color? valueColor;
  212. final VoidCallback? onClear;
  213. final ValueChanged<String> onShowFull;
  214. final TextStyle textStyle;
  215. const _ValueContent({
  216. required this.displayText,
  217. required this.hasValue,
  218. required this.readOnly,
  219. required this.showArrow,
  220. required this.bold,
  221. required this.showMoreOnOverflow,
  222. required this.valueColor,
  223. required this.onClear,
  224. required this.onShowFull,
  225. required this.textStyle,
  226. });
  227. @override
  228. Widget build(BuildContext context) {
  229. final textScaler = MediaQuery.textScalerOf(context);
  230. return LayoutBuilder(
  231. builder: (context, constraints) {
  232. final tp = TextPainter(
  233. text: TextSpan(text: displayText, style: textStyle),
  234. textDirection: TextDirection.ltr,
  235. textScaler: textScaler,
  236. maxLines: 1,
  237. )..layout();
  238. double iconSpace = 0;
  239. if (hasValue && onClear != null) {
  240. iconSpace = 20;
  241. } else if (showArrow && !readOnly) {
  242. iconSpace = 18;
  243. }
  244. final overflows = tp.width > (constraints.maxWidth - iconSpace);
  245. return Row(
  246. children: [
  247. Expanded(
  248. child: Text(
  249. displayText,
  250. maxLines: 1,
  251. softWrap: false,
  252. overflow:
  253. (hasValue && readOnly && showMoreOnOverflow && overflows)
  254. ? TextOverflow.fade
  255. : TextOverflow.ellipsis,
  256. textAlign: TextAlign.end,
  257. style: textStyle,
  258. ),
  259. ),
  260. if (hasValue && onClear != null)
  261. GestureDetector(
  262. onTap: onClear,
  263. child: const Padding(
  264. padding: EdgeInsets.only(left: 4),
  265. child: Icon(Icons.close, size: 16, color: Color(0xFFBBBBBB)),
  266. ),
  267. )
  268. else if (hasValue && readOnly && showMoreOnOverflow && overflows)
  269. GestureDetector(
  270. onTap: () => onShowFull(displayText),
  271. child: const Padding(
  272. padding: EdgeInsets.only(left: 4),
  273. child: Icon(
  274. Icons.more_horiz,
  275. size: 16,
  276. color: Color(0xFFBBBBBB),
  277. ),
  278. ),
  279. )
  280. else if (showArrow && !readOnly) ...[
  281. const SizedBox(width: 4),
  282. Icon(Icons.chevron_right, size: 14, color: Colors.grey),
  283. ],
  284. ],
  285. );
  286. },
  287. );
  288. }
  289. }