为什么FocusNode需要配置在抖动中? [英] Why FocusNode needs to dispose in flutter?

查看:80
本文介绍了为什么FocusNode需要配置在抖动中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在扑打中使用 dispose 时,我有点困惑。是否有关于此的好的教程?

I am kinda very confused when to use dispose in flutter. Are there any good tutorials about this?

在食谱中 https:// flutter.io/docs/cookbook/forms/focus

void dispose() {
  // Clean up the focus node when the Form is disposed
  myFocusNode.dispose();

  super.dispose();
}

为什么我必须调用 myFocusNode.dispose( ); ?如果我不打电话怎么办?

Why do I have to call myFocusNode.dispose();? What happened if I don't call it?

请解释清楚一点,或者给我一些明显的错误代码示例,这些示例在不为FocusNode或其他情况调用dispose时显示不良结果,而不是简单地告诉我这将导致内存泄漏...

Please explain a bit clear or give me some obvious bad code examples showing bad results when not to call dispose for FocusNode or other situations instead of simply telling me this will cause memory leak...

非常感谢。

推荐答案

从文档开始:


焦点节点是长期存在的对象。例如,如果有状态窗口小部件具有可聚焦的子窗口小部件,则应在State.initState方法中创建一个FocusNode,并将其放置在State.dispose方法中,每次State.build方法为可聚焦的子对象提供相同的FocusNode。运行。特别是,每次调用State.build时创建一个FocusNode都会导致每次构建窗口小部件时都失去焦点。

Focus nodes are long-lived objects. For example, if a stateful widget has a focusable child widget, it should create a FocusNode in the State.initState method, and dispose it in the State.dispose method, providing the same FocusNode to the focusable child each time the State.build method is run. In particular, creating a FocusNode each time State.build is invoked will cause the focus to be lost each time the widget is built.

换句话说,布置它们可确保每次构建窗口小部件时节点都能正确调用焦点,其他调用焦点的窗口小部件也不会出现问题,并且还可以保持性能。

In other words, disposing them ensures that the node invokes focus correctly each time the widget is built, there are no issues with other Widgets which invoke focus and it also preserves performance.

这篇关于为什么FocusNode需要配置在抖动中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆