InheritedModel: Add a complete interactive example and update snippet for null safety (#104174)
This commit is contained in:
@@ -34,7 +34,7 @@ import 'framework.dart';
|
||||
/// ```dart
|
||||
/// class MyModel extends InheritedModel<String> {
|
||||
/// // ...
|
||||
/// static MyModel of(BuildContext context, String aspect) {
|
||||
/// static MyModel? of(BuildContext context, String aspect) {
|
||||
/// return InheritedModel.inheritFrom<MyModel>(context, aspect: aspect);
|
||||
/// }
|
||||
/// }
|
||||
@@ -44,37 +44,42 @@ import 'framework.dart';
|
||||
/// be rebuilt when the `foo` aspect of `MyModel` changes. If the aspect
|
||||
/// is null, then the model supports all aspects.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
/// When the inherited model is rebuilt the [updateShouldNotify] and
|
||||
/// [updateShouldNotifyDependent] methods are used to decide what
|
||||
/// should be rebuilt. If [updateShouldNotify] returns true, then the
|
||||
/// inherited model's [updateShouldNotifyDependent] method is tested for
|
||||
/// each dependent and the set of aspect objects it depends on.
|
||||
/// The [updateShouldNotifyDependent] method must compare the set of aspect
|
||||
/// dependencies with the changes in the model itself.
|
||||
///
|
||||
/// For example:
|
||||
/// dependencies with the changes in the model itself. For example:
|
||||
///
|
||||
/// ```dart
|
||||
/// class ABModel extends InheritedModel<String> {
|
||||
/// ABModel({ this.a, this.b, Widget child }) : super(child: child);
|
||||
/// const ABModel({
|
||||
/// super.key,
|
||||
/// this.a,
|
||||
/// this.b,
|
||||
/// required super.child,
|
||||
/// });
|
||||
///
|
||||
/// final int a;
|
||||
/// final int b;
|
||||
/// final int? a;
|
||||
/// final int? b;
|
||||
///
|
||||
/// @override
|
||||
/// bool updateShouldNotify(ABModel old) {
|
||||
/// return a != old.a || b != old.b;
|
||||
/// bool updateShouldNotify(ABModel oldWidget) {
|
||||
/// return a != oldWidget.a || b != oldWidget.b;
|
||||
/// }
|
||||
///
|
||||
/// @override
|
||||
/// bool updateShouldNotifyDependent(ABModel old, Set<String> aspects) {
|
||||
/// return (a != old.a && aspects.contains('a'))
|
||||
/// || (b != old.b && aspects.contains('b'))
|
||||
/// bool updateShouldNotifyDependent(ABModel oldWidget, Set<String> dependencies) {
|
||||
/// return (a != oldWidget.a && dependencies.contains('a'))
|
||||
/// || (b != oldWidget.b && dependencies.contains('b'));
|
||||
/// }
|
||||
///
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// In the previous example the dependencies checked by
|
||||
/// [updateShouldNotifyDependent] are just the aspect strings passed to
|
||||
@@ -84,6 +89,14 @@ import 'framework.dart';
|
||||
/// then changes in the model will cause the widget to be rebuilt
|
||||
/// unconditionally.
|
||||
///
|
||||
/// {@tool dartpad}
|
||||
/// This example shows how to implement [InheritedModel] to rebuild a
|
||||
/// widget based on a qualified dependence. When tapped on the "Resize Logo" button
|
||||
/// only the logo widget is rebuilt while the background widget remains unaffected.
|
||||
///
|
||||
/// ** See code in examples/api/lib/widgets/inherited_model/inherited_model.0.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [InheritedWidget], an inherited widget that only notifies dependents
|
||||
|
||||
Reference in New Issue
Block a user