ソースを参照

upgrade image_picker & fix permission

lucky1213 4 年 前
コミット
de8ed11d1d
共有6 個のファイルを変更した103 個の追加43 個の削除を含む
  1. 1
    1
      example/ios/Flutter/.last_build_id
  2. 4
    4
      example/lib/main.dart
  3. 43
    36
      lib/photo.dart
  4. 38
    0
      lib/src/permission_utils.dart
  5. 15
    1
      pubspec.lock
  6. 2
    1
      pubspec.yaml

+ 1
- 1
example/ios/Flutter/.last_build_id ファイルの表示

@@ -1 +1 @@
1
-894f424b8531a69bd75214bc0c525b8a
1
+627ffd90bffd0082b3a38187f39f4b86

+ 4
- 4
example/lib/main.dart ファイルの表示

@@ -171,8 +171,8 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
171 171
     /// context is required, other params is optional.
172 172
     /// context is required, other params is optional.
173 173
     /// context is required, other params is optional.
174
-    List<AssetPathEntity> pathList2 = await PhotoManager.getAssetPathList(
175
-        hasAll: true, type: RequestType.image,);
174
+    // List<AssetPathEntity> pathList2 = await PhotoManager.getAssetPathList(
175
+    //     hasAll: true, type: RequestType.image,);
176 176
         if (!single) {
177 177
           PickedEntity entity = await PhotoPicker.pickAsset(
178 178
       context: context,
@@ -184,7 +184,7 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
184 184
       thumbSize: 150,
185 185
       sortDelegate: SortDelegate.common,
186 186
       pickType: type,
187
-      photoPathList: pathList2,
187
+      //photoPathList: pathList2,
188 188
     );
189 189
 
190 190
     if (entity == null || entity.asset.isEmpty) {
@@ -213,7 +213,7 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
213 213
             thumbSize: 150,
214 214
             sortDelegate: SortDelegate.common,
215 215
             pickType: type,
216
-            photoPathList: pathList2,
216
+            // photoPathList: pathList2,
217 217
           );
218 218
           print(asset);
219 219
         }

+ 43
- 36
lib/photo.dart ファイルの表示

@@ -1,11 +1,11 @@
1 1
 library photo;
2 2
 
3 3
 import 'dart:async';
4
-import 'dart:io';
5 4
 
6 5
 import 'package:flutter/material.dart';
7 6
 import 'package:image_picker/image_picker.dart';
8 7
 import 'package:photo/src/entity/_theme.dart';
8
+import 'package:photo/src/permission_utils.dart';
9 9
 import 'package:photo/src/ui/page/photo_list_page.dart';
10 10
 import 'package:photo/src/ui/page/photo_main_page.dart';
11 11
 
@@ -17,7 +17,6 @@ import 'package:photo/src/delegate/loading_delegate.dart';
17 17
 import 'package:photo/src/delegate/sort_delegate.dart';
18 18
 import 'package:photo/src/entity/options.dart';
19 19
 import 'package:photo/src/provider/i18n_provider.dart';
20
-import 'package:photo/src/ui/dialog/not_permission_dialog.dart';
21 20
 import 'package:provider/provider.dart';
22 21
 
23 22
 import 'src/provider/config_provider.dart';
@@ -29,7 +28,7 @@ export 'package:photo/src/provider/i18n_provider.dart'
29 28
 export 'package:photo/src/entity/options.dart' show PickType, PickedEntity;
30 29
 export 'package:photo/src/delegate/badge_delegate.dart';
31 30
 export 'package:photo/src/entity/_theme.dart';
32
-export 'package:image_picker/image_picker.dart' show CameraDevice;
31
+export 'package:image_picker/image_picker.dart' show CameraDevice, PickedFile;
33 32
 export 'package:photo_manager/photo_manager.dart' show AssetEntity;
34 33
 
35 34
 class PhotoPicker {
@@ -104,7 +103,6 @@ class PhotoPicker {
104 103
     );
105 104
   }
106 105
 
107
-
108 106
   static Future<AssetEntity> pickSingleAsset({
109 107
     @required BuildContext context,
110 108
     int rowCount = 4,
@@ -139,19 +137,24 @@ class PhotoPicker {
139 137
     return null;
140 138
   }
141 139
 
142
-  static Future<File> pickCamera({
140
+  static Future<PickedFile> pickCamera({
143 141
     double maxWidth,
144 142
     double maxHeight,
145 143
     int imageQuality,
146 144
     CameraDevice preferredCameraDevice = CameraDevice.rear,
147 145
   }) async {
148
-    return ImagePicker.pickImage(
149
-      source: ImageSource.camera,
150
-      maxWidth: maxWidth,
151
-      maxHeight: maxHeight,
152
-      imageQuality: imageQuality,
153
-      preferredCameraDevice: preferredCameraDevice,
154
-    );
146
+    final bool status = await PermissionUtils.canPermission(Permission.camera);
147
+    if (!status) {
148
+      throw 'permission denied';
149
+    } else {
150
+      return ImagePicker().getImage(
151
+        source: ImageSource.camera,
152
+        maxWidth: maxWidth,
153
+        maxHeight: maxHeight,
154
+        imageQuality: imageQuality,
155
+        preferredCameraDevice: preferredCameraDevice,
156
+      );
157
+    }
155 158
   }
156 159
 
157 160
   Future<PickedEntity> _pickAsset(
@@ -161,15 +164,18 @@ class PhotoPicker {
161 164
     List<AssetPathEntity> photoList,
162 165
     List<AssetEntity> pickedAssetList,
163 166
   ) async {
164
-    await requestPermission(context, provider: provider);
165
-
166
-    return _openGalleryContentPage(
167
-      context,
168
-      options,
169
-      provider,
170
-      photoList,
171
-      pickedAssetList,
172
-    );
167
+    final bool status = await PermissionUtils.canMultiplePermission([Permission.photos, Permission.storage]);
168
+    if (!status) {
169
+      throw 'permission denied';
170
+    } else {
171
+      return _openGalleryContentPage(
172
+        context,
173
+        options,
174
+        provider,
175
+        photoList,
176
+        pickedAssetList,
177
+      );
178
+    }
173 179
   }
174 180
 
175 181
   static Widget buildGallery({
@@ -222,21 +228,22 @@ class PhotoPicker {
222 228
     );
223 229
   }
224 230
 
225
-  Future<void> requestPermission(BuildContext context,
226
-      {@required I18nProvider provider}) async {
227
-    var requestPermission = await PhotoManager.requestPermission();
228
-    if (requestPermission != true) {
229
-      var result = await showDialog(
230
-        context: context,
231
-        builder: (ctx) => NotPermissionDialog(
232
-          provider.getNotPermissionText(),
233
-        ),
234
-      );
235
-      if (result == true) {
236
-        PhotoManager.openSetting();
237
-      }
238
-      return null;
239
-    }
231
+  Future<bool> requestPermission({@required I18nProvider provider, @required List<Permission> permissions}) async {
232
+    final bool status =
233
+        await PermissionUtils.canMultiplePermission(permissions);
234
+    // if (!status) {
235
+    //   var result = await showDialog(
236
+    //     context: context,
237
+    //     builder: (ctx) => NotPermissionDialog(
238
+    //       provider.getNotPermissionText(),
239
+    //     ),
240
+    //   );
241
+    //   if (result == true) {
242
+    //     PhotoManager.openSetting();
243
+    //   }
244
+    //   return false;
245
+    // }
246
+    return status;
240 247
   }
241 248
 
242 249
   Future<PickedEntity> _openGalleryContentPage(

+ 38
- 0
lib/src/permission_utils.dart ファイルの表示

@@ -0,0 +1,38 @@
1
+import 'dart:io';
2
+
3
+import 'package:permission_handler/permission_handler.dart';
4
+
5
+export 'package:permission_handler/permission_handler.dart';
6
+
7
+class PermissionUtils {
8
+  static Future<bool> canPermission(Permission permission, [bool request = true]) async {
9
+    if (permission == Permission.storage && Platform.isIOS) {
10
+      return true;
11
+    }
12
+    if (permission == Permission.photos && Platform.isAndroid) {
13
+      return true;
14
+    }
15
+    if (!await permission.status.isGranted) {
16
+      if (request) {
17
+        final PermissionStatus status =  await permission.request();
18
+        if (status.isDenied) {
19
+          openAppSettings();
20
+        }
21
+        return status.isGranted;
22
+      }
23
+      return false;
24
+    } else {
25
+      return true;
26
+    }
27
+  }
28
+
29
+  static Future<bool> canMultiplePermission(List<Permission> list, [bool request = true]) async {
30
+    for (final Permission permission in list) {
31
+      if (!await canPermission(permission, request)) {
32
+        return false;
33
+      }
34
+    }
35
+
36
+    return true;
37
+  }
38
+}

+ 15
- 1
pubspec.lock ファイルの表示

@@ -123,6 +123,20 @@ packages:
123 123
       url: "https://pub.flutter-io.cn"
124 124
     source: hosted
125 125
     version: "1.9.0"
126
+  permission_handler:
127
+    dependency: "direct main"
128
+    description:
129
+      name: permission_handler
130
+      url: "https://pub.flutter-io.cn"
131
+    source: hosted
132
+    version: "5.0.1+1"
133
+  permission_handler_platform_interface:
134
+    dependency: transitive
135
+    description:
136
+      name: permission_handler_platform_interface
137
+      url: "https://pub.flutter-io.cn"
138
+    source: hosted
139
+    version: "2.0.1"
126 140
   photo_manager:
127 141
     dependency: "direct main"
128 142
     description:
@@ -190,7 +204,7 @@ packages:
190 204
       name: test_api
191 205
       url: "https://pub.flutter-io.cn"
192 206
     source: hosted
193
-    version: "0.2.15"
207
+    version: "0.2.16"
194 208
   typed_data:
195 209
     dependency: transitive
196 210
     description:

+ 2
- 1
pubspec.yaml ファイルの表示

@@ -1,6 +1,6 @@
1 1
 name: photo
2 2
 description: image picker, multi picker support video/icloud asset, use flutter as ui, if you want to build custom ui, you just use photo_manager.
3
-version: 0.5.2
3
+version: 0.5.3
4 4
 author: caijinglong<cjl_spy@163.com>
5 5
 homepage: https://github.com/CaiJingLong/flutter_photo
6 6
 
@@ -14,6 +14,7 @@ dependencies:
14 14
   photo_manager: 0.5.1
15 15
   provider: ^4.0.5
16 16
   image_picker: ^0.6.5
17
+  permission_handler: ^5.0.0+hotfix.8
17 18
 
18 19
 dev_dependencies:
19 20
   flutter_test: