android - StatefulWidget flutter 的这个小部件属性是什么

标签 android inheritance flutter frameworks

这是一个非常简单的问题,我有时会看到类似:widget.titlewidget.(anything) in flutter;像 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),),
            ),
        );
    }
}

这实际上是什么?

widget.title 我的意思是,widget 引用的是什么?这是什么?

最佳答案

MyApp 类扩展了 StatefulWidget,这意味着这个小部件存储可变状态。当 MyApp 小部件首次插入树中时,框架调用 createState() 函数来创建 _MyAppState 的新实例以与树中的该位置相关联。 (请注意,State 的子类通常以前导下划线命名,以表明它们是私有(private)实现细节。)当这个小部件的父级重建时,父级会创建 MyApp 的新实例,但框架会重用 _MyAppState 已经在树中的实例,而不是再次调用 createState。

要访问当前MyApp 的属性,_MyAppState 可以使用它的widget 属性。如果父级重建并创建新的 MyApp,则 _MyAppState 将使用新的小部件值重建。如果您希望在小部件属性更改时收到通知,请重写 didUpdateWidget() 函数,该函数作为 oldWidget 传递,以便您将旧小部件与当前小部件进行比较。

现在根据文档:小部件属性

This property is initialized by the framework before calling 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.

reference link

关于android - StatefulWidget flutter 的这个小部件属性是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56679201/

相关文章:

android - 无法在 Android 上将 mp4 文件与 FFmpeg 合并

java - 枚举与类 : code duplication, 组合、扩展、泛型

python - 如何继承查询MongoEngine文档?

java向下转换构造函数调用?

flutter - Flutter Bloc-按钮未触发状态更改

flutter - 使用Dart将String转换为List/Map

java - 多行文本。 libgdx

android - Firebase 离线数据

flutter - 使用 BLoC 在 init 上加载数据

android - 在 Fragment 中获取 not null Context 的保证方法是什么?