InheritedModel / InheritedWidget

https://flutter.institute/sharing-data-inheritedmodel-vs-inheritedwidget/
其工作原理为被其嵌套的子widget可以通过InheritedModel.inheritFrom获取到InheritedModel或者InheritedWidget

https://loveky.github.io/2018/07/18/how-flutter-inheritedwidget-works/

InheritedWidget.png

Snip20190503_1.png

Element源码

this依赖于ancestor
@Deprecated(
    'Use dependOnInheritedElement instead. '
    'This feature was deprecated after v1.12.1.'
  )
  @override
  InheritedWidget inheritFromElement(InheritedElement ancestor, { Object aspect }) {
    return dependOnInheritedElement(ancestor, aspect: aspect);
  }

  @override
  InheritedWidget dependOnInheritedElement(InheritedElement ancestor, { Object aspect }) {
    assert(ancestor != null);
    _dependencies ??= HashSet();
    _dependencies.add(ancestor);
    ancestor.updateDependencies(this, aspect);
    return ancestor.widget;
  }
this依赖于指定类型widget的Element(ancestor),并返回ancestor(也就是指定widget类型的element)的widget
@Deprecated(
    'Use dependOnInheritedWidgetOfExactType instead. '
    'This feature was deprecated after v1.12.1.'
  )
  @override
  InheritedWidget inheritFromWidgetOfExactType(Type targetType, { Object aspect }) {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[targetType];
    if (ancestor != null) {
      assert(ancestor is InheritedElement);
      return inheritFromElement(ancestor, aspect: aspect);
    }
    _hadUnsatisfiedDependencies = true;
    return null;
  }

  @override
  T dependOnInheritedWidgetOfExactType({Object aspect}) {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[T];
    if (ancestor != null) {
      assert(ancestor is InheritedElement);
      return dependOnInheritedElement(ancestor, aspect: aspect);
    }
    _hadUnsatisfiedDependencies = true;
    return null;
  }

查找指定类型widget的Element

@Deprecated(
    'Use getElementForInheritedWidgetOfExactType instead. '
    'This feature was deprecated after v1.12.1.'
  )
  @override
  InheritedElement ancestorInheritedElementForWidgetOfExactType(Type targetType) {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[targetType];
    return ancestor;
  }

  @override
  InheritedElement getElementForInheritedWidgetOfExactType() {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[T];
    return ancestor;
  }
查找指定类型的widget
@Deprecated(
    'Use findAncestorWidgetOfExactType instead. '
    'This feature was deprecated after v1.12.1.'
  )
  @override
  Widget ancestorWidgetOfExactType(Type targetType) {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    Element ancestor = _parent;
    while (ancestor != null && ancestor.widget.runtimeType != targetType)
      ancestor = ancestor._parent;
    return ancestor?.widget;
  }

  @override
  T findAncestorWidgetOfExactType() {
    assert(_debugCheckStateIsActiveForAncestorLookup());
    Element ancestor = _parent;
    while (ancestor != null && ancestor.widget.runtimeType != T)
      ancestor = ancestor._parent;
    return ancestor?.widget;
  }

InheritedModel

由以下代码可知,InheritedModel与InheritedWidget的不同在于

  • InheritedModel将_ dependents的value设置为Set
  • 并且在其他element依赖于InheritedModelElement的时候传递一个T类型的值存储在Set中
  • 当InheritedModelElement notifyDependent的时候会将Set值传递给InheritedModel的updateShouldNotifyDependent方法,在方法中可以判断该依赖于InheritedModelElement的element是否需要刷新。
abstract class InheritedModel extends InheritedWidget {
...
@protected
  bool updateShouldNotifyDependent(covariant InheritedModel oldWidget, Set dependencies);
...
}

class InheritedModelElement extends InheritedElement {
  /// Creates an element that uses the given widget as its configuration.
  InheritedModelElement(InheritedModel widget) : super(widget);

  @override
  InheritedModel get widget => super.widget as InheritedModel;

  @override
  void updateDependencies(Element dependent, Object aspect) {
    final Set dependencies = getDependencies(dependent) as Set;
    if (dependencies != null && dependencies.isEmpty)
      return;

    if (aspect == null) {
      setDependencies(dependent, HashSet());
    } else {
      assert(aspect is T);
      setDependencies(dependent, (dependencies ?? HashSet())..add(aspect as T));
    }
  }

  @override
  void notifyDependent(InheritedModel oldWidget, Element dependent) {
    final Set dependencies = getDependencies(dependent) as Set;
    if (dependencies == null)
      return;
    if (dependencies.isEmpty || widget.updateShouldNotifyDependent(oldWidget, dependencies))
      dependent.didChangeDependencies();
  }
}

你可能感兴趣的:(InheritedModel / InheritedWidget)