Line data Source code
1 : import 'package:collection_providers/collection_providers.dart';
2 : import 'package:flutter/cupertino.dart';
3 :
4 : import 'package:provider/provider.dart';
5 :
6 : class CollectionProvider<T extends CollectionChangeNotifier>
7 : extends ChangeNotifierProvider<T> {
8 : /// Creates a [CollectionProvider] using `create` and automatically
9 : /// dispose it when [CollectionProvider] is removed from the widget tree.
10 : ///
11 : /// `create` must not be `null`.
12 1 : CollectionProvider({
13 : Key key,
14 : @required Create<T> create,
15 : bool lazy,
16 : TransitionBuilder builder,
17 : Widget child,
18 1 : }) : super(
19 : key: key,
20 : create: create,
21 : lazy: lazy,
22 : builder: builder,
23 : child: child,
24 : );
25 :
26 : /// Provides an existing [ChangeNotifier].
27 1 : CollectionProvider.value({
28 : Key key,
29 : @required T value,
30 : TransitionBuilder builder,
31 : Widget child,
32 1 : }) : super.value(
33 : key: key,
34 : builder: builder,
35 : value: value,
36 : child: child,
37 : );
38 :
39 : /// Obtains the nearest [CollectionProvider<T>] up its widget tree and returns
40 : /// its value.
41 : ///
42 : /// If [listen] is `true`, later value changes will trigger a new
43 : /// [State.build] to widgets, and [State.didChangeDependencies] for
44 : /// [StatefulWidget].
45 : ///
46 : /// `listen: false` is necessary to be able to call `CollectionProvider.of`
47 : /// inside [State.initState] or the `create` method of providers.
48 1 : static T of<T extends CollectionChangeNotifier>(
49 : BuildContext context, {
50 : bool listen = true,
51 : }) =>
52 1 : Provider.of<T>(context, listen: listen);
53 : }
|