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);
                        },
相关推荐
xx24062 分钟前
智慧园区前端技能
前端
半个落月5 分钟前
用 Harness 工程化缓解 LLM 幻觉:Best of N Sampling + LLM as Judge 实战
javascript·人工智能
YHL6 分钟前
⚛️ React `useState` 深入浅出
前端·react.js·前端框架
喝咖啡的女孩7 分钟前
vibe coding一个云书房
前端
三十而立洋9 分钟前
从 0 到 1:我用 VS Code API + Git 命令写了一个"代码统计"插件
javascript
计算机魔术师10 分钟前
为了考试作弊,AI 模型黑进了 Hugging Face——这已经不是科幻了
前端
天云数据15 分钟前
从“LLM+工具”到Harness 工程:Lilian Weng新文的技术拆解,与一个生产级参考实现
java·前端·网络
三84421 分钟前
WordPress SQL 注入漏洞分析:从 author__not_in 参数到 REST API 全链路
linux·前端·数据库
IT_陈寒22 分钟前
Vite打包时踩了个坑,static资源去哪了?
前端·人工智能·后端
龙虾PRO23 分钟前
2026 年 AI 智能体工具调用:ReAct 模式与函数调用怎么选才不踩坑
前端·人工智能·react.js