美文网首页
【Flutter】学习InheritedWidget

【Flutter】学习InheritedWidget

作者: popesa | 来源:发表于2022-01-09 15:30 被阅读0次

    官方API文档中有如下一段话:

    Occasionally, the inherited widget is an implementation detail of another class, and is therefore private. The of method in that case is typically put on the public class instead. For example, Theme is implemented as a StatelessWidget that builds a private inherited widget; Theme.of looks for that inherited widget using BuildContext.dependOnInheritedWidgetOfExactType and then returns the ThemeData.

    大致是关于自定义of方法的返回类型问题,返回值类型可以自定义为任意类型,同时定义of方法的类也可以不是InheritedWidget类。
    主要是说明类A不是InheritedWidget也可以定义of方法,此时of方法的返回值自然不是A类型。存在如此设计的主要原因Dart单继承。
    文中以Theme举例说明的,由于Theme继承了StatelessWidget,所以无法成为一个InheritedWidget。Theme中定义的of方法返回值为ThemeData类型。然而ThemeData也不是InheritedWidget的子类,返回的ThemeData实例是从私有类_InheritedTheme实例中获得的。

    相关文章

      网友评论

          本文标题:【Flutter】学习InheritedWidget

          本文链接:https://www.haomeiwen.com/subject/xtvucrtx.html