Flutter 中点击输入框之外的区域,进行失焦,收起键盘

首先引用 FocusNode()

复制代码
FocusNode focusNode = FocusNode();

然后在输入框组件中调用即可

Dart 复制代码
TextField(
                        controller: _textController,
                        textAlign: TextAlign.start,
                        focusNode: focusNode,
                        onTapOutside: (e) => {focusNode.unfocus()},
                        onEditingComplete: () {
                          FocusScope.of(context).requestFocus(focusNode);
                        },
                        decoration: InputDecoration(
                          contentPadding:
                              const EdgeInsets.symmetric(horizontal: 0),
                          hintText: hitText,
                          hintStyle: const TextStyle(color: Colors.grey),
                         
                          focusedBorder: const UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                            borderRadius: BorderRadius.horizontal(),
                          ),
                        ),
                        cursorColor: Colors.white,
                        style: greyTextStyle,
                        onChanged: (value) {
                          setState(() {
                            titleValue = value;
                          });
                        },
                        autofocus: false,
                      ),

下方的代码就是起到点击输入框之外的区域,进行失焦,收起键盘的作用

Dart 复制代码
onTapOutside: (e) => {focusNode.unfocus()},
                        onEditingComplete: () {
                          FocusScope.of(context).requestFocus(focusNode);
                        },
相关推荐
PineappleCoder1 天前
还在重复下载资源?HTTP 缓存让二次访问 “零请求”,用户体验翻倍
前端·性能优化
拉不动的猪1 天前
webpack编译中为什么不建议load替换ast中节点删除consolg.log
前端·javascript·webpack
李姆斯1 天前
Agent时代下,ToB前端的UI和交互会往哪走?
前端·agent·交互设计
源码获取_wx:Fegn08951 天前
基于springboot + vue健身房管理系统
java·开发语言·前端·vue.js·spring boot·后端·spring
闲谈共视1 天前
基于去中心化社交与AI智能服务的Web钱包商业开发的可行性
前端·人工智能·去中心化·区块链
狮恒1 天前
OpenHarmony Flutter 分布式数据管理:跨设备数据同步与一致性保障方案
分布式·flutter·wpf·openharmony
CreasyChan1 天前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
嗝o゚1 天前
鱼与熊掌可兼得?用Flutter+鸿蒙的混合架构破解性能与UI的世纪难题
flutter·架构·harmonyos
JIngJaneIL1 天前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
阿蒙Amon1 天前
JavaScript学习笔记:6.表达式和运算符
javascript·笔记·学习