[英]What is this widget property from the StatefulWidget flutter
Its a very simple question, I sometimes see something like: widget.title
or widget.(anything)
in flutter; 这是一个非常简单的问题,我有时会看到类似于: widget.title
或widget.(anything)
在颤动; like this example in the Text widget child of AppBar
Widget: 像AppBar
Widget的Text小部件子中的这个示例:
class MyApp extends StatefulWidget{
// some declarations here
@override
_MyApp createState() => _MyApp();
}
class _MyApp extends State<MyApp>{
// some declaration here
@override
Widget build(BuildContext context){
return MaterialApp(
home: Scaffold(
appBar: AppBar(child: Text(widget.title),),
),
);
}
}
what does is this actually? 这到底是什么意思?
widget.title
i mean, what is the widget
referencing? widget.title
我的意思是,什么是widget
引用? what is it? 它是什么?
The MyApp
class extends StatefulWidget, which means this widget stores mutable state. MyApp
类扩展了StatefulWidget,这意味着这个小部件存储了可变状态。 When the MyApp widget is first inserted into the tree, the framework calls the createState()
function to create a fresh instance of _MyAppState
to associate with that location in the tree. 当MyApp小部件首次插入树中时,框架会调用createState()
函数来创建_MyAppState
的新实例,以与树中的该位置相关联。 (Notice that subclasses of State are typically named with leading underscores to indicate that they are private implementation details.) When this widget's parent rebuilds, the parent creates a new instance of MyApp
, but the framework reuses the _MyAppState
instance that is already in the tree rather than calling createState again. (请注意,State的子类通常以前导下划线命名,表示它们是私有实现细节。)当此窗口小部件的父窗口重建时,父窗口创建MyApp
的新实例,但框架重用已在树中的_MyAppState
实例而不是再次调用createState。
To access the properties of the current MyApp
, the _MyAppState
can use its widget property
. 要访问当前MyApp
的属性, _MyAppState
可以使用其widget property
。 If the parent rebuilds and creates a new MyApp
, the _MyAppState
rebuilds with the new widget value. 如果父级重建并创建新的MyApp
,则_MyAppState
将使用新的小部件值进行重建。 If you wish to be notified when the widget property changes, override the didUpdateWidget()
function, which is passed as oldWidget to let you compare the old widget with the current widget. 如果您希望在窗口小部件属性更改时收到通知,请覆盖didUpdateWidget()
函数,该函数作为oldWidget传递,以便您将旧窗口小部件与当前窗口小部件进行比较。
Now as Per Docs: widget property 现在作为Per Docs: widget属性
This property is initialized by the framework before calling initState. 在调用initState之前,此属性由框架初始化。 If the parent updates this location in the tree to a new widget with the same runtimeType and Widget.key as the current configuration, the framework will update this property to refer to the new widget and then call didUpdateWidget, passing the old configuration as an argument. 如果父级将树中的此位置更新为具有与当前配置相同的runtimeType和Widget.key的新窗口小部件,则框架将更新此属性以引用新窗口小部件,然后调用didUpdateWidget,将旧配置作为参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.