简体   繁体   English

这是来自StatefulWidget flutter的widget属性是什么

[英]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.titlewidget.(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,将旧配置作为参数传递。

reference link 参考链接

Long answer short 答案很短

You have extended State class 你有扩展的State
Referring docs 参考文档

The State class has a readonly property called widget . State类有一个名为widgetreadonly属性。 Which is what you are referencing to. 这是你引用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何将变量从StatelessWidget传递到StatefulWidget - Flutter: How to pass variables from StatelessWidget to StatefulWidget 我们如何在flutter中获取StatefulWidget的生命周期? 我想确定当前屏幕上小部件的生命周期而不是完整的应用程序 - How Can we get the lifecycle of StatefulWidget in flutter? I want to determine the lifecycle of widget on current screen not of complete app Flutter 中的 Widget 是什么? - What is a Widget in Flutter? 从屏幕底部弹出一个小窗口使用什么 Widget Flutter - What Widget to use for a small Pop Up from the bottom of the screen Flutter 从无状态小部件更新有状态小部件 - Flutter - Updating Stateful Widget from Stateless Widget - Flutter flutter 中的 CustomPaint 小部件的高程属性是否有替代方法? - Is there an alternative for an elevation property for CustomPaint widget in flutter? 在颤动中从父小部件重置子小部件 - Reset child widget from parent widget in flutter Flutter:重定向后循环调用 StatefulWidget - Flutter: StatefulWidget call in loop after redirection 有没有办法将图像从本地存储加载到 Flutter 中 CircleAvatar 小部件的 backgroundImage 属性? - Is there a way for loading images from local storage to the backgroundImage property of the CircleAvatar widget in Flutter? 将数据从 StatelessWidget 传递到 StatefulWidget - Passing data from a StatelessWidget to a StatefulWidget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM