android - 在 Flutter 中访问抽屉时如何阻止文本字段自动聚焦
问题描述
问题是什么
在表单的蓝色下方部分在 my textField
s 中输入内容后,然后以某种方式访问抽屉,以某种方式聚焦最后使用的内容textField
并激活键盘。
预期行为
当抽屉菜单被触发时,TextField
不应该获得焦点并且键盘不应该出现。
旁注
直到我没有在textField
抽屉中输入任何数据之前,操作都可以在不激活键盘的情况下正常工作。
代码
在main.dart
文件中:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
//NavDrawer() is the custom Widget that renders the Drawer
drawer: NavDrawer(),
appBar: AppBar(
...<Normal Appbar Stuff>
),
// BlogHome() is the custom Widget rendering the body
body: BlogHome(),
);
}
}
在BlogHome.dart
文件中
import 'package:flutter/material.dart';
import 'form.dart';
class BlogHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
new TextEditingController().clear();
},
child: Column(
children: <Widget>[
...<Other Widgets>
],
),
),
// This custom widget renders the blue colored form in the bottom
FormData(),
],
),
);
}
}
FormData.dart
文件仅包含两个普通文本字段及其在有状态小部件中的样式。
解决方案
在BlogHome.dart
文件中
import 'package:flutter/material.dart';
import 'form.dart';
class BlogHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
// This line is unfocusing the current context that is calling unfocus upon itself
//But what it needs to do is call unfocus upon the primary focus
//So, commenting this line
//FocusScope.of(context).unfocus();
// This is the correct approach of calling unfocus on primary focus
FocusManager.instance.primaryFocus.unfocus();
new TextEditingController().clear();
},
child: Column(
children: <Widget>[
...<Other Widgets>
],
),
),
// This custom widget renders the blue colored form in the bottom
FormData(),
],
),
);
}
}
这是@Kashifa
您绝对正确的正确解决方案。
推荐阅读
- mysql - 将两个查询合二为一,条件不同
- r - 如何在 r 中分别导出不同大小的列表及其名称?
- reactjs - React Highcharts 多次触发 setExtremes 事件
- tensorflow - 程序在测试 Tensorflow-gpu 2.0.0 的最后一步崩溃
- android - 从 API 响应创建动态 strings.xml 文件
- c# - x64 中的 COM 异常:检索具有 CLSID {} 的组件的 COM 类工厂失败,原因是以下错误:80080005
- java - 使用 JPA 实现树迭代器
- csv - 使用 Thingsboard 2.2-PE 导出 XLS/CSV 数据
- python - 如何使用水平对齐的 2 个点(打开和填充)创建 matplotlib 图例条目?
- android - 我的应用程序没有在分屏中启动 - 为什么?