LCOV - code coverage report
Current view: top level - src - property_change_consumer.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 5 5 100.0 %
Date: 2021-09-17 16:22:12 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/widgets.dart';
       2             : import 'package:property_change_notifier/property_change_notifier.dart';
       3             : 
       4             : typedef PropertyChangeBuilder<T, S> = Widget Function(BuildContext, T?, Set<S>?);
       5             : 
       6             : /// A widget-based listener for cases where a [BuildContext] is hard to access, or if you prefer this kind of API.
       7             : /// To register the widget to be rebuilt only on specific property changes, provide a [properties] parameter.
       8             : /// The type parameter [T] is the type of the [PropertyChangeNotifier] subclass.
       9             : /// The type parameter [S] is the type of the properties to observe.
      10             : ///
      11             : /// Access both the model value and the changed properties via the [builder] callback:
      12             : /// ```dart
      13             : /// PropertyChangeConsumer<MyModel, String>(
      14             : ///    properties: ['foo', 'bar'],
      15             : ///    builder: (context, model, properties) {
      16             : ///      return Column(
      17             : ///        children: [
      18             : ///          Text('$properties were changed!'),
      19             : ///          RaisedButton(
      20             : ///            child: Text('Update foo'),
      21             : ///            onPressed: () {
      22             : ///              model.foo = DateTime.now().toString();
      23             : ///            },
      24             : ///          ),
      25             : ///          RaisedButton(
      26             : ///            child: Text('Update bar'),
      27             : ///            onPressed: () {
      28             : ///              model.bar = DateTime.now().toString();
      29             : ///            },
      30             : ///          ),
      31             : ///        ],
      32             : ///      );
      33             : ///    },
      34             : ///  );
      35             : ///
      36             : /// See also:
      37             : ///
      38             : ///  * [StringPropertyChangeConsumer], where the second generic type can be omitted
      39             : ///    for models that extend PropertyChangeNotifier<String>.
      40             : ///
      41             : /// ```
      42             : class PropertyChangeConsumer<T extends PropertyChangeNotifier<S>, S extends Object> extends StatelessWidget {
      43             :   final Iterable<S>? properties;
      44             :   final PropertyChangeBuilder<T, S> builder;
      45             : 
      46           1 :   const PropertyChangeConsumer({
      47             :     Key? key,
      48             :     this.properties,
      49             :     required this.builder,
      50           1 :   })  : super(key: key);
      51             : 
      52           1 :   @override
      53             :   Widget build(BuildContext context) {
      54           2 :     final model = PropertyChangeProvider.of<T, S>(context, properties: properties, listen: true);
      55           4 :     return builder(context, model?.value, model?.properties);
      56             :   }
      57             : }
      58             : 
      59             : /// A convenience typedef to use in the common use case where property names are of type [String].
      60             : typedef StringPropertyChangeConsumer<T extends PropertyChangeNotifier<String>> = PropertyChangeConsumer<T, String>;

Generated by: LCOV version 1.14