Flutter 在颤振中单击文本字段中的phone back按钮后,如何隐藏颤振上的输入键盘?
我想在编辑文本字段中点击phone back按钮时隐藏键盘。但当我尝试时,这个键盘会在重新打开后快速关闭。我该如何解决这个问题?我在Nexus6和Pixel2上试过这个,同样的东西Flutter 在颤振中单击文本字段中的phone back按钮后,如何隐藏颤振上的输入键盘?,flutter,dart,Flutter,Dart,我想在编辑文本字段中点击phone back按钮时隐藏键盘。但当我尝试时,这个键盘会在重新打开后快速关闭。我该如何解决这个问题?我在Nexus6和Pixel2上试过这个,同样的东西 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('page', style: (TextStyle(fontSize: 25))), ), b
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('page', style: (TextStyle(fontSize: 25))),
),
body: Center(
child: TextField(
autofocus: false,
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: '--',
),
),
),
);
}
编辑:我们需要代码。但是我认为文本字段的自动聚焦属性设置为true。你需要把它改成假的 编辑后: 你需要使用Willposcope
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBackPressed(context),
child: Scaffold(
appBar: AppBar(
title: Text('page', style: (TextStyle(fontSize: 25))),
),
body: Center(
child: TextField(
autofocus: false,
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: '--',
),
),
),
);
}
Future _onBackPressed(BuildContext context) {
FocusScope.of(context).unfocus();
}
使用WillPopScope在backpress事件时关闭键盘我添加了建议。这对我不起作用。我想用文本字段中的设备后退按钮(键盘下)关闭键盘。我想用“后退”按钮从文本字段中删除焦点。您可以按照以下建议操作。我看到了这篇文章,但它不是为设备返回按钮,它只是在文本字段外点击,失去焦点。它很有用,但不是每次,用户可能会尝试返回按钮的设备失去焦点。
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(.....),
onWillPop: () async {
var currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
currentFocus.focusedChild.unfocus();
return false;
}else{
return true;
}
},
);
}