You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter点击TextField页面自动返回主页问题及解决

问题:Flutter输入文本时自动返回主页

我正在为大学作业开发一个Flutter项目,其中有一个创建项目页面,用户需输入项目名称等信息完成创建。但每当我尝试使用TextField输入文本时,应用就会返回主页。

相关代码片段:

Container(
                margin: EdgeInsets.only(
                  left: 24 / 360 * ScreenConstants.screenWidth,
                  top: 25 / 360 * ScreenConstants.screenWidth,
                ),
                alignment: Alignment.centerLeft,
                child: Text(
                  "Project Name",
                  style: Theme.of(context).textTheme.titleMedium,
                  textAlign: TextAlign.left,
                ),
              ),
              InkWell(
                onTap: () {
                  _projectNameFocusNode.requestFocus();
                },
                child: TextFormField(
                  focusNode: _projectNameFocusNode,
                  controller: _projectNameController,
                  // Rest of your TextFormField configuration
                ),
              )
            ],

我尝试过使用上述InkWell、GestureDetector以及单独的TextField,但问题始终存在。


编辑1:页面跳转日志

I/ImeTracker( 9719): com.example.kaptur_alpha_v1:31b874ef: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT
D/InputMethodManager( 9719): showSoftInput() view=io.flutter.embedding.android.FlutterView{a6c6e42 VFE...... .F...... 0,0-1080,2154 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
D/InsetsController( 9719): show(ime(), fromIme=true)
D/EGL_emulation( 9719): app_time_stats: avg=42.17ms min=29.73ms max=57.70ms count=24
D/EGL_emulation( 9719): app_time_stats: avg=468.49ms min=1.20ms max=13982.47ms count=30

编辑2:Widget树截图

  • 主页Widget树截图
  • 新建项目页面Widget树截图
  • 点击TextField后的Widget树截图

编辑3:应用重建确认

Suat的观察非常准确,点击TextField后应用会自行重建。通过main方法中的打印语句确认应用是重建而非重启,测试显示应用重建了四次。


最终更新:问题已解决

我的好友Abhijith参考Suat的建议检查代码后,发现问题出在main函数中使用了UniqueKey()构造函数。

每次状态变化调用build方法时,基础应用关联的Key会不断改变,导致整个应用从主页重新构建。

解决方法:

  • 直接移除key参数
  • 或者将UniqueKey()保存到一个变量中,再传入key参数

在此将该线程标记为已关闭,感谢大家的时间与付出🙏🏽!

内容的提问来源于stack exchange,提问作者kris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:40:19