当我专注于文本字段时,我需要知道如何隐藏提示文本。这是我的代码:
class _ContactoState extends State<Contacto> {
FocusNode focusMsj;
@override
void initState() {
super.initState();
focusMsj = FocusNode();
focusMsj.addListener(() {
if (!focusMsj.hasFocus) {
FocusScope.of(context).requestFocus(focusMsj);
}
});
}
TextField(
focusNode: focusMsj,
hintText: focusMsj.hasFocus ? ' ' : 'Hint Text',)
return WillPopScope(
child: Listener(
onPointerUp: (e) {
focusMsj.hasFocus ? FocusScope.of(context).requestFocus(FocusNode()): '';
},
谢谢
答案 0 :(得分:3)
有一个属性:
TextField(decoration: InputDecoration(hasFloatingPlaceholder: false));
编辑:不建议使用上述版本,新版本为:
TextField(decoration: InputDecoration(floatingLabelBehavior: FloatingLabelBehavior.never,),),
答案 1 :(得分:1)
为此,您需要做类似的事情
class Play extends StatefulWidget {
@override
_PlayState createState() => _PlayState();
}
class _PlayState extends State<Play> {
FocusNode focusNode = FocusNode();
String hintText = 'Hello , iam Hint';
@override
void initState() {
// TODO: implement initState
super.initState();
focusNode.addListener(() {
if (focusNode.hasFocus) {
hintText = '';
} else {
hintText = 'Hello , iam Hint';
}
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
print(focusNode.hasFocus);
}),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
focusNode: focusNode,
decoration: InputDecoration(
hintText: hintText,
),
),
TextField(
decoration: InputDecoration(
hintText: '!!',
),
),
],
),
),
);
}
}
不久,我通过其focusNode属性收听了TextField。当TextField具有焦点时,我使hintText属性等于空字符串值
答案 2 :(得分:0)
您可以尝试的一种简单解决方案是使用 FloatingBehavior.never 定义 labelText
TextField(
decoration: InputDecoration(
labelText: "Search",
floatingLabelBehavior: FloatingLabelBehavior.never,
)
)
HintText 将在未聚焦时显示。聚焦时,提示文字将消失。