labelText属性 https://api.flutter.dev/flutter/material/InputDecoration/labelText.html 当文本字段处于焦点状态并且您具有这样的Inputdecoration时向上移动:
InputDecoration(
labelText: labelText,
filled: true,
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: ImpexColors.grey,
width: 1.0,
),
),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: ImpexColors.secondaryColor,
width: 2.0,
),
),
labelStyle: TextStyle(
color: ImpexColors.blue,
),
)
然后在上边框的中间对齐。 聚焦TextField时,是否可以更改labelText的位置?
答案 0 :(得分:0)
有可能,孩子吗?
FocusNode myFocusNode = FocusNode();
...
TextFormField(
focusNode: myFocusNode,
decoration: InputDecoration(
labelStyle: TextStyle(
height: myFocusNode.hasFocus ? 0.1 : 1, // 0,1 - label will sit on top of border
),
labelText: labelText,
...
));
还必须添加:
onTap: () => setState(() {FocusScope.of(context).requestFocus(myFocusNode);})