- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 Emily Fortuna 的文章(和视频)中,她提到:
GlobalKeys have two uses: they allow widgets to change parents anywhere in your app without losing state, or they can be used to access information about another widget in a completely different part of the widget tree. An example of the first scenario might if you wanted to show the same widget on two different screens, but holding all the same state, you’d want to use a GlobalKey.
她的文章包含一个名为“使用 GlobalKey 重用 Widget”的应用程序的 gif 演示,但没有提供源代码(可能是因为它太简单了)。您还可以在此处观看快速视频演示,从 8:30 开始:https://youtu.be/kn0EOS-ZiIc?t=510
我如何实现她的演示?我在哪里定义 GlobalKey 变量以及我如何/在哪里使用它?基本上,例如,我想显示一个每秒计数的计数器,并将其显示在许多不同的屏幕上。这是 GlobalKey 可以帮助我的事情吗?
最佳答案
使用 GlobalKey
的最常见用例在树周围移动小部件是有条件地将“子”包装到另一个小部件中,如下所示:
Widget build(context) {
if (foo) {
return Foo(child: child);
}
return child;
}
使用这样的代码,您很快就会注意到,如果 child
是有状态的,切换 foo
将使 child
失去其状态,即通常出乎意料。
为了解决这个问题,我们会让我们的小部件有状态,创建一个 GlobalKey
, 并将 child
包装成 KeyedSubtree
.
这是一个例子:
class Example extends StatefulWidget {
const Example({Key key, this.foo, this.child}) : super(key: key);
final Widget child;
final bool foo;
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
final key = GlobalKey();
@override
Widget build(BuildContext context) {
final child = KeyedSubtree(key: key, child: widget.child);
if (widget.foo) {
return Foo(child: child);
}
return child;
}
}
关于flutter - 如何在改变 parent 时使用 `GlobalKey` 来维护小部件的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895273/
我基本上想做的(我确实做了,详情在前面)是创建一个具有多种形式的屏幕,使用按钮 (NEXT) 更改其内容。所以我有 3 种形式,当我按下下一步时,我保存第一种形式并传递给另一种形式,当我把我的类(cl
从 Screen A 导航到 Screen B 并单击“取消”按钮返回后,我遇到了 globalKey 错误到屏幕A。 似乎问题在于 Screen B 要么是 A) 未正确处理 B) 没有做本来可以做
我正在使用 Flutter。我有一个带有 3 个标签的简单应用程序。每个选项卡中都有一个带有 ListView 的 RefreshIndicator。这些行是用另一种方法构建的。这是代码: @ove
通常是 Widget 的键是var key = GlobalKey但是TextFormFieldState是私有(private)的。由于我只有一种文本形式,因此我避免将其包装在 Form 中.可以吗
我想使用 GlobalKey 保持子部件状态父状态改变后。有一种解决方法是使用 Opacity为了解决问题,不知道为什么GlobalKey在这种情况下不能按预期工作。 import 'dart:asy
我的类提供程序类中有一个类似这样的 showSnackbar 方法: GlobalKey scaffoldKey = GlobalKey(debugLabel: 'scaffoldKe
我的“主要”小部件必须具有全局键。如果我使用 pushNamed 或等效项导航到它,它会生成有关小部件树中重复键的异常。我只能弹出到这个小部件,但这严重减少了我的导航选项和小部件的可重用性。 我已经包
许多与构建 Flutter UI 相关的问题归结为错误的 BuildContext(例如显示 SnackBar)。答案通常提供使用 Builder 或使用 GlobalKey。两者都有效,但我注意到
我在 flutter 中看到了一个示例,您可以在其中导航路线而无需访问 BuildContext。 final GlobalKey _navKey = new GlobalKey(); navigat
我想知道 GlobalKey 之间的区别,更重要的是现实世界的用法区别。和 GlobalObjectKey ,什么时候使用哪个,什么时候避免? 最佳答案 唯一的区别是他们的==运算符(operator
我有一个 ListView ,其中有一张卡片作为 ListView 的 subview 。该卡片包含一个带有表单键的 TextFormField。因为它是一个 ListView ,所以它使用相同的表单
我试图在启动应用程序时设置一个初始索引,方法是通过共享首选项检索该索引。但是我有这个错误信息: I/flutter (19911): ══╡ EXCEPTION CAUGHT BY WIDGETS L
我有一张带有标记循环的 map ,对于每个标记,我都尝试显示带有位置名称的 snackbar 。我有一个类似的代码。 static final GlobalKey scaffoldKey =
我正在尝试根据这个 github 问题在 flutter 中实现自定义脚手架:https://github.com/flutter/flutter/issues/19606 import 'packa
在我的 Flutter 应用程序中,我需要知道当前路径以避免两次推送相同的路径。 我发现我可以使用下面的代码: ModalRoute.of(context).settings.name 它有效
我试图解决这个问题,我在 Stack Overflow 中查找了答案 但是我还没解决 我在创建和更新页面中使用了全局键 我做了什么 我尝试将静态添加到全局键,但我做不到因为我无法将 key 包装在 r
我是新来的 Provider package.并且只是为了学习目的制作演示应用程序。 这是我的简单 Form Widget 代码。 1) 注册页面 (我的应用程序在哪里启动) class Regist
在 Emily Fortuna 的文章(和视频)中,她提到: GlobalKeys have two uses: they allow widgets to change parents anywhe
我发现的将项目插入 Flutter AnimatedList 的所有示例都利用 GlobalKey.currentState 告诉 AnimatedList 小部件已插入项目。从列表中删除也是如此。
我正在开发一个页面使用 ReorderableListView 小部件的应用程序。向上拖动 Tile 时出现错误,但是当我向下拖动任何 Tile 时它工作正常。该错误与多个小部件使用的全局 key 有
我是一名优秀的程序员,十分优秀!