flutter - 在导航回上一个屏幕之前无法隐藏/处置键盘
问题描述
我的目标只是从我所在的屏幕返回而不重建上一页上的小部件,我发现只要打开键盘,前一个屏幕中的小部件就会重建,但如果我通过点击后退按钮手动关闭键盘在我的手机上然后导航回小部件状态已恢复,所以我只想在返回上一页之前关闭键盘
页面示例
import 'package:flutter/material.dart';
class ScreenTwo extends StatefulWidget {
final Function testFunction;
final String title;
const ScreenTwo({required this.testFunction, required this.title});
@override
_ScreenTwoState createState() => _ScreenTwoState();
}
class _ScreenTwoState extends State<ScreenTwo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.title,
),
),
body: Column(
children: [
GestureDetector(
child: Center(child: Text("Back")),
onTap: () {
Navigator.pop(context);
widget.testFunction(false);
},
),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), hintText: 'Enter a search term'),
)
],
),
);
}
}
上一篇文章中商定的解决方案是FocusScopeNode
,但它对我不起作用,我想 100% 确定键盘在我返回之前已关闭/处置。
GestureDetector(
child: Center(child: Text("Back")),
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
Navigator.pop(context);
widget.testFunction(false);
},
),
解决方案
推荐阅读
- python - 为什么 Jupyter Notebook 上的 Voila 会出现 403 错误?
- azure - 显示“在模块中找到命令,但无法加载模块”的错误消息。
- c++ - 计算积分的算法
- python - python - 我可以在图表上现有的y轴旁边创建另一个y轴吗?
- quickfixn - 消息解析不起作用(Linux - Ubuntu)
- java - SslContext getInstance() 未找到本机 openssl 实现
- junit - 找不到:java.lang.NoClassDefFoundError:org/springframework/test/context/TestContextAnnotationUtils
- windows - Windows 服务停止工作代码 1053
- mysql - mysqldump 返回未知字符
- python - 使用python lxlm解析多个子目录下的多个xml文件