@@ -1696,6 +1696,56 @@ void main() {
1696
1696
expect (states, < String > ['deactivate' , 'dispose' ]);
1697
1697
});
1698
1698
1699
+ testWidgetsWithLeakTracking ('Element.deactivate reports its deactivation to the InheritedElement it depends on' , (WidgetTester tester) async {
1700
+ final List <Key > removedDependentWidgetKeys = < Key > [];
1701
+
1702
+ InheritedElement elementCreator (InheritedWidget widget) {
1703
+ return _InheritedElementSpy (
1704
+ widget,
1705
+ onRemoveDependent: (Element dependent) {
1706
+ removedDependentWidgetKeys.add (dependent.widget.key! );
1707
+ },
1708
+ );
1709
+ }
1710
+
1711
+ Widget builder (BuildContext context) {
1712
+ context.dependOnInheritedWidgetOfExactType <Inherited >();
1713
+ return Container ();
1714
+ }
1715
+
1716
+ await tester.pumpWidget (
1717
+ Inherited (
1718
+ 0 ,
1719
+ elementCreator: elementCreator,
1720
+ child: Column (
1721
+ children: < Widget > [
1722
+ Builder (
1723
+ key: const Key ('dependent' ),
1724
+ builder: builder,
1725
+ ),
1726
+ ],
1727
+ ),
1728
+ ),
1729
+ );
1730
+
1731
+ expect (removedDependentWidgetKeys, isEmpty);
1732
+
1733
+ await tester.pumpWidget (
1734
+ Inherited (
1735
+ 0 ,
1736
+ elementCreator: elementCreator,
1737
+ child: Column (
1738
+ children: < Widget > [
1739
+ Container (),
1740
+ ],
1741
+ ),
1742
+ ),
1743
+ );
1744
+
1745
+ expect (removedDependentWidgetKeys, hasLength (1 ));
1746
+ expect (removedDependentWidgetKeys.first, const Key ('dependent' ));
1747
+ });
1748
+
1699
1749
testWidgetsWithLeakTracking ('RenderObjectElement.unmount disposes of its renderObject' , (WidgetTester tester) async {
1700
1750
await tester.pumpWidget (const Placeholder ());
1701
1751
final RenderObjectElement element = tester.allElements.whereType <RenderObjectElement >().last;
@@ -1902,12 +1952,33 @@ class DirtyElementWithCustomBuildOwner extends Element {
1902
1952
}
1903
1953
1904
1954
class Inherited extends InheritedWidget {
1905
- const Inherited (this .value, {super .key, required super .child});
1955
+ const Inherited (this .value, {super .key, required super .child, this .elementCreator });
1906
1956
1907
1957
final int ? value;
1958
+ final InheritedElement Function (Inherited widget)? elementCreator;
1908
1959
1909
1960
@override
1910
1961
bool updateShouldNotify (Inherited oldWidget) => oldWidget.value != value;
1962
+
1963
+ @override
1964
+ InheritedElement createElement () {
1965
+ if (elementCreator != null ) {
1966
+ return elementCreator !(this );
1967
+ }
1968
+ return super .createElement ();
1969
+ }
1970
+ }
1971
+
1972
+ class _InheritedElementSpy extends InheritedElement {
1973
+ _InheritedElementSpy (super .widget, {this .onRemoveDependent});
1974
+
1975
+ final void Function (Element element)? onRemoveDependent;
1976
+
1977
+ @override
1978
+ void removeDependent (Element dependent) {
1979
+ super .removeDependent (dependent);
1980
+ onRemoveDependent? .call (dependent);
1981
+ }
1911
1982
}
1912
1983
1913
1984
class DependentStatefulWidget extends StatefulWidget {
0 commit comments