Flutter页面在我点击文本字段时关闭。

huangapple go评论91阅读模式
英文:

Flutter page closes when I tap on the text field

问题

[FINAL UPDATE - 问题已解决]

亲爱的朋友,Abhijith 查看了代码,根据Suat 的建议指出,我在主函数中使用了UniqueKey()构造函数。

这导致每当状态发生变化并调用build方法时,与基本应用程序关联的键都会发生变化,这导致整个应用程序从主屏幕重新构建。

简单地移除键参数,或者在将其传递给键参数之前,将UniqueKey()保存在键变量中,如下所示,也可以解决问题。

由此,我将此线程标记为关闭,并感谢大家为此付出的时间和努力 🎉🙏。干杯!

[原始帖子]

我一直在为我的大学项目工作中开发一个Flutter项目。在我的项目中,有一个创建项目页面,用户需要输入一些详细信息,包括项目名称,以继续创建。但是,每当我尝试使用TextField输入一些文本时,应用程序都会将我带回主屏幕。请参考以下演示以供参考:

演示

这是有问题的代码。我尝试使用了如上所示的InkWell、GestureDetector和单独的TextField。但无论我做什么,错误都不断重复。

你们能帮我弄清楚这里发生了什么吗?非常感谢!

英文:

[FINAL UPDATE - Problem Solved]

A dear friend of mine, Abhijith took a look at the code following the inputs from Suat and noted that I have been using a UniqueKey() constructor in my main function.

Flutter页面在我点击文本字段时关闭。

This made it so that each time there was a State change and the build method was called, the key associated with the basic app kept changing, which caused the whole app to rebuild from the home screen.

Simply removing the key parameter, or saving the UniqueKey() in a key variable as shown below, before passing it into the key parameter will also do the trick.

Flutter页面在我点击文本字段时关闭。

That being said, I'm marking this thread as closed and I thank everyone for all your time and effort 🙏🏽. Cheers!

[Original Post]

I have been working on a Flutter project for my college work. In my project there is a create project page, where the user is supposed to enter some details including the project name to proceed with the creation. But whenever I try to enter some text using a TextField, the application takes me back to the home screen. Please see the following demonstration for reference

Demonstration

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
                ),
              )
            ],

This is the code in question. I have tried using InkWell as above, GestureDetector, and TextField by itself. But whatever I do, the error keeps repeating itself.

Can you guys help me figure out what's going on here? Thanks a lot!

[Edit 1]
This is the logs I get as the control flows outside the page

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

[Edit 2]

Including the screenshots of the widget tree during different states.

Flutter页面在我点击文本字段时关闭。

Flutter页面在我点击文本字段时关闭。

Flutter页面在我点击文本字段时关闭。

[Edit 3]

Suat's observation was spot on. The app rebuilds itself for some reason after the text field is clicked. A print statement included in the main method confirms that the app is indeed rebuilding and not restarting.

This image shows the testing code included and the corresponding output. I believe the output suggests that the application rebuilds itself four times.
Flutter页面在我点击文本字段时关闭。

答案1

得分: 4

移除ChangeNotifierProvider中的UniqueKey()。这将导致Widget重新构建。

英文:

Remove the UniqueKey() in ChangeNotifierProvider. It will lead to the rebuilding of the Widget.

答案2

得分: 1

我遇到了类似的问题,然后我检查了Flutter Inspector,发现我不小心一次又一次地将前一页推送了,这会让你以为当前页正在弹出。我建议你检查一下你的导航堆栈是否有类似的错误。
下面我添加了一个示例截图。在这里,MyHomePage被意外地推送了很多次。

Flutter页面在我点击文本字段时关闭。

英文:

I was facing a similar issue then I checked Flutter Inspector and saw that I was accidentally pushing the previous page over and over again, which makes you think that the current page is popping. I suggest you to check your navigation stack for a similar bug.
I am adding a sample screenshot below. Here MyHomePage is accidentally being pushed many times.

Flutter页面在我点击文本字段时关闭。

huangapple
  • 本文由 发表于 2023年6月26日 07:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定