Documentation Updated
1 |
import 'package:flutter/foundation.dart'; |
|
2 |
import 'package:flutter/widgets.dart'; |
|
3 |
|
|
4 |
import 'listenable_provider.dart' show ListenableProvider; |
|
5 |
import 'provider.dart'; |
|
6 |
|
|
7 |
/// Listens to a [ValueListenable] and expose its current value.
|
|
8 |
class ValueListenableProvider<T> |
|
9 |
extends DeferredInheritedProvider<ValueListenable<T>, T> { |
|
10 |
/// Creates a [ValueNotifier] using [create] and automatically dispose it
|
|
11 |
/// when [ValueListenableProvider] is removed from the tree.
|
|
12 |
///
|
|
13 |
/// [create] must not be `null`.
|
|
14 |
///
|
|
15 |
/// {@macro provider.updateshouldnotify}
|
|
16 |
///
|
|
17 |
/// See also:
|
|
18 |
///
|
|
19 |
/// * [ValueListenable]
|
|
20 |
/// * [ListenableProvider], similar to [ValueListenableProvider] but for any
|
|
21 |
/// kind of [Listenable].
|
|
22 | 3 |
ValueListenableProvider({ |
23 |
Key key, |
|
24 |
@required Create<ValueNotifier<T>> create, |
|
25 |
UpdateShouldNotify<T> updateShouldNotify, |
|
26 |
bool lazy, |
|
27 |
TransitionBuilder builder, |
|
28 |
Widget child, |
|
29 | 3 |
}) : super( |
30 |
key: key, |
|
31 |
create: create, |
|
32 |
lazy: lazy, |
|
33 |
builder: builder, |
|
34 |
updateShouldNotify: updateShouldNotify, |
|
35 | 3 |
startListening: _startListening(), |
36 |
dispose: _dispose, |
|
37 |
child: child, |
|
38 |
);
|
|
39 |
|
|
40 |
/// Listens to [value] and exposes its current value.
|
|
41 |
///
|
|
42 |
/// Changing [value] will stop listening to the previous [value] and listen
|
|
43 |
/// the new one. Removing [ValueListenableProvider] from the tree will also
|
|
44 |
/// stop listening to [value].
|
|
45 |
///
|
|
46 |
/// ```dart
|
|
47 |
/// ValueListenable<int> foo;
|
|
48 |
///
|
|
49 |
/// ValueListenableProvider<int>.value(
|
|
50 |
/// valueListenable: foo,
|
|
51 |
/// child: Container(),
|
|
52 |
/// );
|
|
53 |
/// ```
|
|
54 | 3 |
ValueListenableProvider.value({ |
55 |
Key key, |
|
56 |
@required ValueListenable<T> value, |
|
57 |
UpdateShouldNotify<T> updateShouldNotify, |
|
58 |
TransitionBuilder builder, |
|
59 |
Widget child, |
|
60 | 3 |
}) : super.value( |
61 |
key: key, |
|
62 |
builder: builder, |
|
63 |
value: value, |
|
64 |
updateShouldNotify: updateShouldNotify, |
|
65 | 3 |
startListening: _startListening(), |
66 |
child: child, |
|
67 |
);
|
|
68 |
|
|
69 | 3 |
static void _dispose(BuildContext context, ValueListenable<Object> notifier) { |
70 | 3 |
if (notifier is ValueNotifier) { |
71 | 3 |
notifier.dispose(); |
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 | 3 |
static DeferredStartListening<ValueListenable<T>, T> _startListening<T>() { |
76 | 3 |
return (_, setState, controller, __) { |
77 | 3 |
setState(controller.value); |
78 |
|
|
79 | 3 |
final listener = () => setState(controller.value); |
80 | 3 |
controller.addListener(listener); |
81 | 3 |
return () => controller.removeListener(listener); |
82 |
};
|
|
83 |
}
|
|
84 |
}
|
Read our documentation on viewing source code .