Line data Source code
1 : import 'package:flutter/widgets.dart'; 2 : import 'package:property_change_notifier/property_change_notifier.dart'; 3 : 4 : /// A widget-based listener for cases where a [BuildContext] is hard to access, or if you prefer this kind of API. 5 : /// To register the widget to be rebuilt only on specific property changes, provide a [properties] parameter. 6 : /// 7 : /// Access both the model value and the changed property via the [builder] callback: 8 : /// ```dart 9 : /// PropertyChangeConsumer<MyModel>( 10 : /// properties: ['foo', 'bar'], 11 : /// builder: (context, model, property) { 12 : /// return Column( 13 : /// children: [ 14 : /// Text('$property was changed!'), 15 : /// RaisedButton( 16 : /// child: Text('Update foo'), 17 : /// onPressed: () { 18 : /// model.foo = DateTime.now().toString(); 19 : /// }, 20 : /// ), 21 : /// RaisedButton( 22 : /// child: Text('Update bar'), 23 : /// onPressed: () { 24 : /// model.bar = DateTime.now().toString(); 25 : /// }, 26 : /// ), 27 : /// ], 28 : /// ); 29 : /// }, 30 : /// ); 31 : /// ``` 32 : class PropertyChangeConsumer<T extends PropertyChangeNotifier> extends StatelessWidget { 33 : final Iterable<Object> properties; 34 : final Widget Function(BuildContext, T, Object) builder; 35 : 36 1 : PropertyChangeConsumer({ 37 : Key key, 38 : this.properties, 39 : @required this.builder, 40 1 : }) : assert(builder != null), 41 1 : super(key: key); 42 : 43 1 : @override 44 : Widget build(BuildContext context) { 45 2 : final model = PropertyChangeProvider.of<T>(context, properties: this.properties, listen: true); 46 3 : return this.builder(context, model.value, model.property); 47 : } 48 : }