太长,读不下去了
使用
InputDecoration.label
并通过a
Text
你自己而不是使用
InputDecoration.labelText
它接受一个被椭圆化的字符串。
让我们来看看
TextFormField
小部件源代码:
return UnmanagedRestorationScope(
bucket: field.bucket,
child: TextField(
restorationId: restorationId,
controller: state._effectiveController,
focusNode: focusNode,
decoration: effectiveDecoration.copyWith(errorText: field.errorText),
https://github.com/flutter/flutter/blob/78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9/packages/flutter/lib/src/material/text_form_field.dart#L200C31-L200C31
使用
TextField
小装置。可以。那么,我们来深入了解一下:
if (widget.decoration != null) {
child = AnimatedBuilder(
animation: Listenable.merge(<Listenable>[ focusNode, controller ]),
builder: (BuildContext context, Widget? child) {
return InputDecorator(
decoration: _getEffectiveDecoration(),
baseStyle: widget.style,
textAlign: widget.textAlign,
textAlignVertical: widget.textAlignVertical,
https://github.com/flutter/flutter/blob/78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9/packages/flutter/lib/src/material/text_field.dart#L1448C33-L1448C33
使用
InputDecorator
用于显示装饰(包含您的标签)。让我们看看这个:
final Widget? label = decoration.labelText == null && decoration.label == null ? null : _Shaker(
animation: _shakingLabelController.view,
child: AnimatedOpacity(
duration: _kTransitionDuration,
curve: _kTransitionCurve,
opacity: _shouldShowLabel ? 1.0 : 0.0,
child: AnimatedDefaultTextStyle(
duration:_kTransitionDuration,
curve: _kTransitionCurve,
style: widget._labelShouldWithdraw
? _getFloatingLabelStyle(themeData, defaults)
: labelStyle,
child: decoration.label ?? Text(
decoration.labelText!,
overflow: TextOverflow.ellipsis,
textAlign: textAlign,
),
),
),
);
https://github.com/flutter/flutter/blob/4bc7a44eb69056a6744f576c2cea656d65b656e2/packages/flutter/lib/src/material/input_decorator.dart#L2245-L2249
对。如果
输入装饰标签
如果为空,那么它确实通过了你的
labelText
文本
具有以下功能的小部件
overflow
明确设置为
ellipsis
。但是如果我们把
label
那么,房地产呢?
TextField(
decoration: InputDecoration(
label: Text(lipsum),
),
)
我们得到了一个多行标签! :)
从中吸取了一个小教训:深入了解Flutter的源代码!这真的是一个知识、内幕信息的来源,也是一种更深入地了解Flutter如何工作和做事的方法,以及在这个问题的情况下,如何解决问题。