Kaynağa Gözat

add provider

lucky1213 4 yıl önce
ebeveyn
işleme
defd7e941e

+ 21
- 13
lib/photo.dart Dosyayı Görüntüle

@@ -5,6 +5,7 @@ import 'dart:async';
5 5
 import 'package:flutter/material.dart';
6 6
 import 'package:photo/src/entity/_theme.dart';
7 7
 import 'package:photo/src/ui/page/photo_list_page.dart';
8
+import 'package:photo/src/ui/page/photo_main_page.dart';
8 9
 
9 10
 import 'package:photo_manager/photo_manager.dart';
10 11
 
@@ -15,7 +16,7 @@ import 'package:photo/src/delegate/sort_delegate.dart';
15 16
 import 'package:photo/src/entity/options.dart';
16 17
 import 'package:photo/src/provider/i18n_provider.dart';
17 18
 import 'package:photo/src/ui/dialog/not_permission_dialog.dart';
18
-import 'package:photo/src/ui/photo_app.dart';
19
+import 'package:provider/provider.dart';
19 20
 
20 21
 import 'src/provider/config_provider.dart';
21 22
 export 'package:photo/src/delegate/checkbox_builder_delegate.dart';
@@ -165,18 +166,17 @@ class PhotoPicker {
165 166
     assert(context != null, "context must be not null");
166 167
     assert(pickType != null, "pickType must be not null");
167 168
 
168
-    final pickerProvider = PhotoPickerProvider(
169
-      provider: provider,
170
-      options: options,
171
-      pickedAssetList: pickedAssetList,
169
+    return ChangeNotifierProvider(
170
+      create: (_) => PhotoProvider(
171
+        provider: provider,
172
+        options: options,
173
+        pickedAssetList: pickedAssetList,
174
+      ),
172 175
       child: PhotoListPage(
173 176
         onSelected: onSelected,
174 177
         options: options,
175
-        photoList: photoPathList,
176 178
       ),
177 179
     );
178
-
179
-    return pickerProvider;
180 180
   }
181 181
 
182 182
   Future<void> requestPermission(BuildContext context, {@required I18nProvider provider}) async {
@@ -204,11 +204,19 @@ class PhotoPicker {
204 204
   ) async {
205 205
     return Navigator.of(context, rootNavigator: true).push(
206 206
       MaterialPageRoute(
207
-        builder: (ctx) => PhotoApp(
208
-          options: options,
209
-          provider: provider,
210
-          photoList: photoList,
211
-          pickedAssetList: pickedAssetList,
207
+        builder: (ctx) => ChangeNotifierProvider(
208
+          create: (_) => PhotoProvider(
209
+            provider: provider,
210
+            options: options,
211
+            pickedAssetList: pickedAssetList,
212
+          ),
213
+          child: PhotoMainPage(
214
+            onClose: (List<AssetEntity> value) {
215
+              Navigator.pop(context, value);
216
+            },
217
+            options: options,
218
+            photoList: photoList,
219
+          ),
212 220
         ),
213 221
       ),
214 222
     );

+ 8
- 14
lib/src/provider/config_provider.dart Dosyayı Görüntüle

@@ -5,27 +5,21 @@ import 'package:photo/src/provider/i18n_provider.dart';
5 5
 import 'package:photo_manager/photo_manager.dart';
6 6
 
7 7
 // ignore_for_file: deprecated_member_use
8
-class PhotoPickerProvider extends InheritedWidget {
8
+
9
+class PhotoProvider extends ChangeNotifier {
9 10
   final Options options;
10 11
   final I18nProvider provider;
11 12
   final AssetProvider assetProvider = AssetProvider();
12 13
   final List<AssetEntity> pickedAssetList;
13
-  PhotoPickerProvider({
14
+  PhotoProvider({
14 15
     @required this.options,
15 16
     @required this.provider,
16
-    @required Widget child,
17
-    this.pickedAssetList,
18
-    Key key,
19
-  }) : super(key: key, child: child);
17
+    List<AssetEntity> pickedAssetList,
18
+  }) : this.pickedAssetList = pickedAssetList ?? [];
19
+  
20 20
 
21 21
   @override
22
-  bool updateShouldNotify(InheritedWidget oldWidget) {
23
-    return true;
22
+  void notifyListeners() {
23
+    super.notifyListeners();
24 24
   }
25
-
26
-  static PhotoPickerProvider of(BuildContext context) =>
27
-      context.inheritFromWidgetOfExactType(PhotoPickerProvider);
28
-
29
-  static AssetProvider assetProviderOf(BuildContext context) =>
30
-      of(context).assetProvider;
31 25
 }

+ 28
- 28
lib/src/ui/page/photo_list_page.dart Dosyayı Görüntüle

@@ -6,6 +6,7 @@ import 'package:photo/src/provider/gallery_list_provider.dart';
6 6
 import 'package:photo/src/provider/selected_provider.dart';
7 7
 import 'package:photo/src/provider/asset_provider.dart';
8 8
 import 'package:photo/src/provider/config_provider.dart';
9
+import 'package:provider/provider.dart';
9 10
 
10 11
 import 'photo_main_page.dart';
11 12
 import 'photo_preview_page.dart';
@@ -14,13 +15,11 @@ import 'photo_preview_page.dart';
14 15
 class PhotoListPage extends StatefulWidget {
15 16
   final ValueChanged<List<AssetEntity>> onSelected;
16 17
   final ListOptions options;
17
-  final List<AssetPathEntity> photoList;
18 18
 
19 19
   const PhotoListPage({
20 20
     Key key,
21 21
     this.onSelected,
22 22
     this.options,
23
-    this.photoList,
24 23
   }) : super(key: key);
25 24
 
26 25
   @override
@@ -29,9 +28,10 @@ class PhotoListPage extends StatefulWidget {
29 28
 
30 29
 class _PhotoListPageState extends State<PhotoListPage>
31 30
     with SelectedProvider, GalleryListProvider, WidgetsBindingObserver {
31
+  PhotoProvider get config => Provider.of<PhotoProvider>(context, listen: false);
32 32
   ListOptions get options => widget.options;
33
-  I18nProvider get i18nProvider => PhotoPickerProvider.of(context).provider;
34
-  AssetProvider get assetProvider => PhotoPickerProvider.of(context).assetProvider;
33
+  I18nProvider get i18nProvider => config.provider;
34
+  AssetProvider get assetProvider => config.assetProvider;
35 35
   List<AssetEntity> get list => assetProvider.data;
36 36
 
37 37
   bool _isInit = false;
@@ -48,31 +48,31 @@ class _PhotoListPageState extends State<PhotoListPage>
48 48
     super.initState();
49 49
     scaffoldKey = GlobalKey();
50 50
     scrollController = ScrollController();
51
-    // WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
52
-    //   var requestPermission = await PhotoManager.requestPermission();
53
-    //   if (requestPermission != true) {
54
-    //     var result = await showDialog(
55
-    //       context: context,
56
-    //       builder: (ctx) => NotPermissionDialog(
57
-    //         provider.getNotPermissionText(),
58
-    //       ),
59
-    //     );
60
-    //     if (result == true) {
61
-    //       PhotoManager.openSetting();
62
-    //     }
63
-    //     return null;
64
-    //   }
65
-    // });
51
+    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
52
+      // var requestPermission = await PhotoManager.requestPermission();
53
+      // if (requestPermission != true) {
54
+      //   var result = await showDialog(
55
+      //     context: context,
56
+      //     builder: (ctx) => NotPermissionDialog(
57
+      //       provider.getNotPermissionText(),
58
+      //     ),
59
+      //   );
60
+      //   if (result == true) {
61
+      //     PhotoManager.openSetting();
62
+      //   }
63
+      //   return null;
64
+      // }
65
+      if (!_isInit) {
66
+        final pickedList = config.pickedAssetList ?? [];
67
+        addPickedAsset(pickedList.toList());
68
+        _refreshList();
69
+      }
70
+    });
66 71
   }
67 72
 
68 73
   @override
69 74
   void didChangeDependencies() {
70 75
     super.didChangeDependencies();
71
-    if (!_isInit) {
72
-      final pickedList = PhotoPickerProvider.of(context).pickedAssetList ?? [];
73
-      addPickedAsset(pickedList.toList());
74
-      _refreshList();
75
-    }
76 76
   }
77 77
 
78 78
   @override
@@ -160,7 +160,8 @@ class _PhotoListPageState extends State<PhotoListPage>
160 160
   @override
161 161
   Widget build(BuildContext context) {
162 162
     if (!_isInit) {
163
-      return _buildLoading();
163
+      return Container();
164
+      // return _buildLoading();
164 165
     }
165 166
     final noMore = assetProvider.noMore;
166 167
 
@@ -279,9 +280,8 @@ class _PhotoListPageState extends State<PhotoListPage>
279 280
     Navigator.of(context).push(
280 281
       MaterialPageRoute(
281 282
         builder: (ctx) {
282
-          return PhotoPickerProvider(
283
-            provider: PhotoPickerProvider.of(context).provider,
284
-            options: options,
283
+          return ChangeNotifierProvider.value(
284
+            value: config,
285 285
             child: PhotoPreviewPage(
286 286
               selectedProvider: this,
287 287
               list: List.of(list),

+ 81
- 69
lib/src/ui/page/photo_main_page.dart Dosyayı Görüntüle

@@ -17,6 +17,7 @@ import 'package:photo/src/ui/dialog/change_gallery_dialog.dart';
17 17
 import 'package:photo/src/ui/page/main/empty_widget.dart';
18 18
 import 'package:photo/src/ui/page/photo_preview_page.dart';
19 19
 import 'package:photo_manager/photo_manager.dart';
20
+import 'package:provider/provider.dart';
20 21
 
21 22
 part './main/bottom_widget.dart';
22 23
 part './main/image_item.dart';
@@ -39,11 +40,11 @@ class PhotoMainPage extends StatefulWidget {
39 40
 
40 41
 class _PhotoMainPageState extends State<PhotoMainPage>
41 42
     with SelectedProvider, GalleryListProvider {
43
+  PhotoProvider get config =>
44
+      Provider.of<PhotoProvider>(context, listen: false);
42 45
   Options get options => widget.options;
43
-
44
-  I18nProvider get i18nProvider => PhotoPickerProvider.of(context).provider;
45
-  AssetProvider get assetProvider =>
46
-      PhotoPickerProvider.of(context).assetProvider;
46
+  I18nProvider get i18nProvider => config.provider;
47
+  AssetProvider get assetProvider => config.assetProvider;
47 48
 
48 49
   List<AssetEntity> get list => assetProvider.data;
49 50
 
@@ -81,7 +82,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
81 82
   void didChangeDependencies() {
82 83
     super.didChangeDependencies();
83 84
     if (!_isInit) {
84
-      final pickedList = PhotoPickerProvider.of(context).pickedAssetList ?? [];
85
+      final pickedList = config.pickedAssetList ?? [];
85 86
       addPickedAsset(pickedList.toList());
86 87
       _refreshList();
87 88
     }
@@ -130,16 +131,16 @@ class _PhotoMainPageState extends State<PhotoMainPage>
130 131
               Material(
131 132
                 color: options.theme.surfaceColor,
132 133
                 child: Container(
133
-                  height: MediaQuery.of(context).size.height * 0.5 - MediaQuery.of(context).padding.top,
134
+                  height: MediaQuery.of(context).size.height * 0.5 -
135
+                      MediaQuery.of(context).padding.top,
134 136
                   child: ChangeGalleryDialog(
135
-                    galleryList: galleryPathList,
136
-                    i18n: i18nProvider,
137
-                    options: options,
138
-                    current: assetProvider.current,
139
-                    onGalleryChange: (AssetPathEntity result) {
140
-                      _onGalleryChange?.call(result);
141
-                    }
142
-                  ),
137
+                      galleryList: galleryPathList,
138
+                      i18n: i18nProvider,
139
+                      options: options,
140
+                      current: assetProvider.current,
141
+                      onGalleryChange: (AssetPathEntity result) {
142
+                        _onGalleryChange?.call(result);
143
+                      }),
143 144
                 ),
144 145
               ),
145 146
             ],
@@ -169,41 +170,47 @@ class _PhotoMainPageState extends State<PhotoMainPage>
169 170
             splashColor: Colors.transparent,
170 171
             highlightColor: Colors.transparent,
171 172
             child: Container(
172
-              alignment: Alignment.center,
173
-              height: 44.0,
174
-              padding: EdgeInsets.zero.copyWith(right: kToolbarHeight),
175
-              child: Row(
176
-                mainAxisAlignment: MainAxisAlignment.center,
177
-                children: [
178
-                  Text(currentGalleryName, style: options.theme.textStyle.apply(
179
-                    fontSizeDelta: 2.0,
180
-                    fontWeightDelta: 1,
181
-                  ),),
182
-                  Container(
183
-                    width: 18,
184
-                    height: 18,
185
-                    margin: EdgeInsets.only(left: 8),
186
-                    decoration: ShapeDecoration(shape: CircleBorder(
187
-                      side: BorderSide(
188
-                        color: options.theme.onSurfaceColor,
189
-                        width: 1.5
173
+                alignment: Alignment.center,
174
+                height: 44.0,
175
+                padding: EdgeInsets.zero.copyWith(right: kToolbarHeight),
176
+                child: Row(
177
+                  mainAxisAlignment: MainAxisAlignment.center,
178
+                  children: [
179
+                    Text(
180
+                      currentGalleryName,
181
+                      style: options.theme.textStyle.apply(
182
+                        fontSizeDelta: 2.0,
183
+                        fontWeightDelta: 1,
190 184
                       ),
191
-                    )),
192
-                    child: AnimatedSwitcher(
193
-                      transitionBuilder: (child, animation){
194
-                        final Tween<double> tween = overlayEntry != null ? Tween(begin: -0.25, end: 0.25) : Tween(begin: 0.25, end: 0.75);
195
-                        //var tween = Tween(begin: -0.5, end: 0.0);
196
-                        return RotationTransition(child: child, turns: tween.animate(animation));
197
-                      },
198
-                      duration: Duration(milliseconds: 200),
199
-                      reverseDuration: Duration(milliseconds: 0),
200
-                      child: Icon(Icons.chevron_left, key: ValueKey(overlayEntry == null ? 0 : 1),  size: 14, color: options.theme.onSurfaceColor),
201 185
                     ),
202
-                  ),
203
-                ],
204
-              )
205
-              
206
-            ),
186
+                    Container(
187
+                      width: 18,
188
+                      height: 18,
189
+                      margin: EdgeInsets.only(left: 8),
190
+                      decoration: ShapeDecoration(
191
+                          shape: CircleBorder(
192
+                        side: BorderSide(
193
+                            color: options.theme.onSurfaceColor, width: 1.5),
194
+                      )),
195
+                      child: AnimatedSwitcher(
196
+                        transitionBuilder: (child, animation) {
197
+                          final Tween<double> tween = overlayEntry != null
198
+                              ? Tween(begin: -0.25, end: 0.25)
199
+                              : Tween(begin: 0.25, end: 0.75);
200
+                          //var tween = Tween(begin: -0.5, end: 0.0);
201
+                          return RotationTransition(
202
+                              child: child, turns: tween.animate(animation));
203
+                        },
204
+                        duration: Duration(milliseconds: 200),
205
+                        reverseDuration: Duration(milliseconds: 0),
206
+                        child: Icon(Icons.chevron_left,
207
+                            key: ValueKey(overlayEntry == null ? 0 : 1),
208
+                            size: 14,
209
+                            color: options.theme.onSurfaceColor),
210
+                      ),
211
+                    ),
212
+                  ],
213
+                )),
207 214
           ),
208 215
         ),
209 216
         body: _buildBody(),
@@ -248,7 +255,8 @@ class _PhotoMainPageState extends State<PhotoMainPage>
248 255
       SnackBar(
249 256
         content: Text(
250 257
           msg,
251
-          style: options.theme.textStyle.copyWith(color: options.theme.onPrimaryColor),
258
+          style: options.theme.textStyle
259
+              .copyWith(color: options.theme.onPrimaryColor),
252 260
         ),
253 261
         duration: Duration(milliseconds: 1500),
254 262
         backgroundColor: options.theme.primaryColor.withOpacity(0.7),
@@ -326,17 +334,19 @@ class _PhotoMainPageState extends State<PhotoMainPage>
326 334
     return Container(
327 335
       color: options.theme.backgroundColor,
328 336
       padding: EdgeInsets.all(5),
329
-      child: assetProvider.count == 0 ? DefaultEmptyDelegate().buildEmpty(context, options) : GridView.builder(
330
-        controller: scrollController,
331
-        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
332
-          crossAxisCount: options.rowCount,
333
-          childAspectRatio: options.itemRadio,
334
-          crossAxisSpacing: options.padding,
335
-          mainAxisSpacing: options.padding,
336
-        ),
337
-        itemBuilder: _buildItem,
338
-        itemCount: count,
339
-      ),
337
+      child: assetProvider.count == 0
338
+          ? DefaultEmptyDelegate().buildEmpty(context, options)
339
+          : GridView.builder(
340
+              controller: scrollController,
341
+              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
342
+                crossAxisCount: options.rowCount,
343
+                childAspectRatio: options.itemRadio,
344
+                crossAxisSpacing: options.padding,
345
+                mainAxisSpacing: options.padding,
346
+              ),
347
+              itemBuilder: _buildItem,
348
+              itemCount: count,
349
+            ),
340 350
     );
341 351
   }
342 352
 
@@ -401,7 +411,9 @@ class _PhotoMainPageState extends State<PhotoMainPage>
401 411
   Widget _buildText(AssetEntity entity) {
402 412
     var isSelected = containsEntity(entity);
403 413
     Widget child;
404
-    BoxDecoration decoration = BoxDecoration(borderRadius: BorderRadius.circular(1.0),);
414
+    BoxDecoration decoration = BoxDecoration(
415
+      borderRadius: BorderRadius.circular(1.0),
416
+    );
405 417
     if (isSelected) {
406 418
       child = Text(
407 419
         (indexOfSelected(entity) + 1).toString(),
@@ -413,9 +425,11 @@ class _PhotoMainPageState extends State<PhotoMainPage>
413 425
       );
414 426
       decoration = decoration.copyWith(color: options.theme.primaryColor);
415 427
     } else {
416
-      decoration = decoration.copyWith(border: Border.all(
417
-        color: options.theme.onPrimaryColor.withAlpha(200),
418
-      ),);
428
+      decoration = decoration.copyWith(
429
+        border: Border.all(
430
+          color: options.theme.onPrimaryColor.withAlpha(200),
431
+        ),
432
+      );
419 433
     }
420 434
     return Padding(
421 435
       padding: const EdgeInsets.all(8.0),
@@ -452,9 +466,8 @@ class _PhotoMainPageState extends State<PhotoMainPage>
452 466
     Navigator.of(context).push(
453 467
       MaterialPageRoute(
454 468
         builder: (ctx) {
455
-          return PhotoPickerProvider(
456
-            provider: PhotoPickerProvider.of(context).provider,
457
-            options: options,
469
+          return ChangeNotifierProvider.value(
470
+            value: config,
458 471
             child: PhotoPreviewPage(
459 472
               selectedProvider: this,
460 473
               list: List.of(list),
@@ -482,9 +495,8 @@ class _PhotoMainPageState extends State<PhotoMainPage>
482 495
     isPushed = true;
483 496
     var v = await Navigator.of(context).push(
484 497
       MaterialPageRoute(
485
-        builder: (ctx) => PhotoPickerProvider(
486
-          provider: PhotoPickerProvider.of(context).provider,
487
-          options: options,
498
+        builder: (ctx) => ChangeNotifierProvider.value(
499
+          value: config,
488 500
           child: PhotoPreviewPage(
489 501
             selectedProvider: this,
490 502
             list: List.of(selectedList),

+ 2
- 1
lib/src/ui/page/photo_preview_page.dart Dosyayı Görüntüle

@@ -9,6 +9,7 @@ import 'package:photo/src/provider/config_provider.dart';
9 9
 import 'package:photo/src/provider/selected_provider.dart';
10 10
 import 'package:photo/src/ui/page/photo_main_page.dart';
11 11
 import 'package:photo_manager/photo_manager.dart';
12
+import 'package:provider/provider.dart';
12 13
 
13 14
 class PhotoPreviewPage extends StatefulWidget {
14 15
   final SelectedProvider selectedProvider;
@@ -44,7 +45,7 @@ class PhotoPreviewPage extends StatefulWidget {
44 45
 }
45 46
 
46 47
 class _PhotoPreviewPageState extends State<PhotoPreviewPage> {
47
-  PhotoPickerProvider get config => PhotoPickerProvider.of(context);
48
+  PhotoProvider get config => Provider.of<PhotoProvider>(context, listen: false);
48 49
   AssetProvider get assetProvider => widget.assetProvider;
49 50
 
50 51
   Options get options => config.options;

+ 0
- 40
lib/src/ui/photo_app.dart Dosyayı Görüntüle

@@ -1,40 +0,0 @@
1
-import 'package:flutter/material.dart';
2
-
3
-import 'package:photo_manager/photo_manager.dart';
4
-
5
-import 'package:photo/src/entity/options.dart';
6
-import 'package:photo/src/provider/config_provider.dart';
7
-import 'package:photo/src/provider/i18n_provider.dart';
8
-import 'package:photo/src/ui/page/photo_main_page.dart';
9
-
10
-class PhotoApp extends StatelessWidget {
11
-  final Options options;
12
-  final I18nProvider provider;
13
-  final List<AssetPathEntity> photoList;
14
-  final List<AssetEntity> pickedAssetList;
15
-  const PhotoApp({
16
-    Key key,
17
-    this.options,
18
-    this.provider,
19
-    this.photoList,
20
-    this.pickedAssetList,
21
-  }) : super(key: key);
22
-
23
-  @override
24
-  Widget build(BuildContext context) {
25
-    final pickerProvider = PhotoPickerProvider(
26
-      provider: provider,
27
-      options: options,
28
-      pickedAssetList: pickedAssetList,
29
-      child: PhotoMainPage(
30
-        onClose: (List<AssetEntity> value) {
31
-          Navigator.pop(context, value);
32
-        },
33
-        options: options,
34
-        photoList: photoList,
35
-      ),
36
-    );
37
-
38
-    return pickerProvider;
39
-  }
40
-}

+ 15
- 0
pubspec.lock Dosyayı Görüntüle

@@ -88,6 +88,13 @@ packages:
88 88
       url: "https://pub.flutter-io.cn"
89 89
     source: hosted
90 90
     version: "1.1.8"
91
+  nested:
92
+    dependency: transitive
93
+    description:
94
+      name: nested
95
+      url: "https://pub.flutter-io.cn"
96
+    source: hosted
97
+    version: "0.0.4"
91 98
   path:
92 99
     dependency: transitive
93 100
     description:
@@ -109,6 +116,13 @@ packages:
109 116
       url: "https://pub.flutter-io.cn"
110 117
     source: hosted
111 118
     version: "0.5.1-dev.4"
119
+  provider:
120
+    dependency: "direct main"
121
+    description:
122
+      name: provider
123
+      url: "https://pub.flutter-io.cn"
124
+    source: hosted
125
+    version: "4.0.5"
112 126
   quiver:
113 127
     dependency: transitive
114 128
     description:
@@ -186,3 +200,4 @@ packages:
186 200
     version: "3.6.1"
187 201
 sdks:
188 202
   dart: ">=2.7.0 <3.0.0"
203
+  flutter: ">=1.12.1"

+ 1
- 0
pubspec.yaml Dosyayı Görüntüle

@@ -12,6 +12,7 @@ dependencies:
12 12
     sdk: flutter
13 13
 
14 14
   photo_manager: ^0.5.1-dev.2
15
+  provider: ^4.0.5
15 16
   # photo_manager:
16 17
   #   git:
17 18
   #     url: https://github.com/CaiJingLong/flutter_photo_manager.git