Просмотр исходного кода

Merge pull request #89 from CaiJingLong/fix-refresh-for-asset-list

Fix: refresh from assetPathList.
Caijinglong 5 лет назад
Родитель
Сommit
347423e765
No account linked to committer's email address

+ 2
- 1
example/android/gradle.properties Просмотреть файл

@@ -1,3 +1,4 @@
1 1
 org.gradle.jvmargs=-Xmx1536M
2 2
 android.enableJetifier=true
3
-android.useAndroidX=true
3
+android.useAndroidX=true
4
+android.enableR8=true

+ 17
- 14
example/lib/main.dart Просмотреть файл

@@ -1,22 +1,25 @@
1 1
 import 'package:flutter/cupertino.dart';
2 2
 import 'package:flutter/material.dart';
3
+import 'package:oktoast/oktoast.dart';
3 4
 import 'package:photo/photo.dart';
4 5
 import 'package:photo_manager/photo_manager.dart';
5 6
 
6 7
 import './preview.dart';
7 8
 
8
-void main() => runApp(new MyApp());
9
+void main() => runApp(MyApp());
9 10
 
10 11
 class MyApp extends StatelessWidget {
11 12
   // This widget is the root of your application.
12 13
   @override
13 14
   Widget build(BuildContext context) {
14
-    return new MaterialApp(
15
-      title: 'Pick Image Demo',
16
-      theme: new ThemeData(
17
-        primarySwatch: Colors.blue,
15
+    return OKToast(
16
+      child: MaterialApp(
17
+        title: 'Pick Image Demo',
18
+        theme: ThemeData(
19
+          primarySwatch: Colors.blue,
20
+        ),
21
+        home: MyHomePage(title: 'Pick Image Demo'),
18 22
       ),
19
-      home: new MyHomePage(title: 'Pick Image Demo'),
20 23
     );
21 24
   }
22 25
 }
@@ -26,7 +29,7 @@ class MyHomePage extends StatefulWidget {
26 29
   final String title;
27 30
 
28 31
   @override
29
-  _MyHomePageState createState() => new _MyHomePageState();
32
+  _MyHomePageState createState() => _MyHomePageState();
30 33
 }
31 34
 
32 35
 class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
@@ -62,9 +65,9 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
62 65
 
63 66
   @override
64 67
   Widget build(BuildContext context) {
65
-    return new Scaffold(
66
-      appBar: new AppBar(
67
-        title: new Text(widget.title),
68
+    return Scaffold(
69
+      appBar: AppBar(
70
+        title: Text(widget.title),
68 71
         actions: <Widget>[
69 72
           FlatButton(
70 73
             child: Icon(Icons.image),
@@ -96,10 +99,10 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
96 99
           ),
97 100
         ),
98 101
       ),
99
-      floatingActionButton: new FloatingActionButton(
102
+      floatingActionButton: FloatingActionButton(
100 103
         onPressed: () => _pickAsset(PickType.all),
101 104
         tooltip: 'pickImage',
102
-        child: new Icon(Icons.add),
105
+        child: Icon(Icons.add),
103 106
       ),
104 107
     );
105 108
   }
@@ -158,8 +161,8 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
158 161
       photoPathList: pathList,
159 162
     );
160 163
 
161
-    if (imgList == null) {
162
-      currentSelected = "not select item";
164
+    if (imgList == null || imgList.isEmpty) {
165
+      showToast("No pick item.");
163 166
     } else {
164 167
       List<String> r = [];
165 168
       for (var e in imgList) {

+ 2
- 6
example/pubspec.yaml Просмотреть файл

@@ -23,33 +23,29 @@ dependencies:
23 23
   photo:
24 24
     path: ../
25 25
 
26
+  oktoast: ^2.0.0
27
+
26 28
 dev_dependencies:
27 29
   flutter_test:
28 30
     sdk: flutter
29 31
 
30
-
31 32
 # For information on the generic Dart part of this file, see the
32 33
 # following page: https://www.dartlang.org/tools/pub/pubspec
33 34
 
34 35
 # The following section is specific to Flutter.
35 36
 flutter:
36
-
37 37
   # The following line ensures that the Material Icons font is
38 38
   # included with your application, so that you can use the icons in
39 39
   # the material Icons class.
40 40
   uses-material-design: true
41
-
42 41
   # To add assets to your application, add an assets section, like this:
43 42
   # assets:
44 43
   #  - images/a_dot_burr.jpeg
45 44
   #  - images/a_dot_ham.jpeg
46
-
47 45
   # An image asset can refer to one or more resolution-specific "variants", see
48 46
   # https://flutter.io/assets-and-images/#resolution-aware.
49
-
50 47
   # For details regarding adding assets from package dependencies, see
51 48
   # https://flutter.io/assets-and-images/#from-packages
52
-
53 49
   # To add custom fonts to your application, add a fonts section here,
54 50
   # in this "flutter" section. Each entry in this list should have a
55 51
   # "family" key with the font family name, and a "fonts" key with a

+ 1
- 1
example/test/widget_test.dart Просмотреть файл

@@ -12,7 +12,7 @@ import '../lib/main.dart';
12 12
 void main() {
13 13
   testWidgets('Counter increments smoke test', (WidgetTester tester) async {
14 14
     // Build our app and trigger a frame.
15
-    await tester.pumpWidget(new MyApp());
15
+    await tester.pumpWidget(MyApp());
16 16
 
17 17
     // Verify that our counter starts at 0.
18 18
     expect(find.text('0'), findsOneWidget);

+ 1
- 1
lib/src/engine/lru_cache.dart Просмотреть файл

@@ -40,7 +40,7 @@ class _ImageCacheEntity {
40 40
 typedef EvictionHandler<K, V>(K key, V value);
41 41
 
42 42
 class LRUMap<K, V> {
43
-  final LinkedHashMap<K, V> _map = new LinkedHashMap<K, V>();
43
+  final LinkedHashMap<K, V> _map = LinkedHashMap<K, V>();
44 44
   final int _maxSize;
45 45
   final EvictionHandler<K, V> _handler;
46 46
 

+ 17
- 17
lib/src/ui/page/photo_main_page.dart Просмотреть файл

@@ -81,7 +81,6 @@ class _PhotoMainPageState extends State<PhotoMainPage>
81 81
   @override
82 82
   void initState() {
83 83
     super.initState();
84
-    _refreshList();
85 84
     scaffoldKey = GlobalKey();
86 85
     scrollController = ScrollController();
87 86
     _changeThrottle = Throttle(onCall: _onAssetChange);
@@ -89,6 +88,14 @@ class _PhotoMainPageState extends State<PhotoMainPage>
89 88
     PhotoManager.startChangeNotify();
90 89
   }
91 90
 
91
+  @override
92
+  void didChangeDependencies() {
93
+    super.didChangeDependencies();
94
+    if (!_isInit) {
95
+      _refreshList();
96
+    }
97
+  }
98
+
92 99
   @override
93 100
   void dispose() {
94 101
     PhotoManager.removeChangeCallback(_changeThrottle.call);
@@ -187,7 +194,8 @@ class _PhotoMainPageState extends State<PhotoMainPage>
187 194
     );
188 195
   }
189 196
 
190
-  void _refreshList() {
197
+  void _refreshList() async {
198
+    await Future.delayed(Duration.zero);
191 199
     if (!useAlbum) {
192 200
       _refreshListFromWidget();
193 201
       return;
@@ -197,15 +205,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
197 205
   }
198 206
 
199 207
   Future<void> _refreshListFromWidget() async {
200
-    galleryPathList.clear();
201
-    galleryPathList.addAll(widget.photoList);
202
-    this.list.clear();
203
-    var assetList = await galleryPathList[0].assetList;
204
-    _sortAssetList(assetList);
205
-    this.list.addAll(assetList);
206
-    setState(() {
207
-      _isInit = true;
208
-    });
208
+    _onRefreshAssetPathList(widget.photoList);
209 209
   }
210 210
 
211 211
   Future<void> _refreshListFromGallery() async {
@@ -221,6 +221,10 @@ class _PhotoMainPageState extends State<PhotoMainPage>
221 221
         pathList = await PhotoManager.getAssetPathList();
222 222
     }
223 223
 
224
+    _onRefreshAssetPathList(pathList);
225
+  }
226
+
227
+  Future<void> _onRefreshAssetPathList(List<AssetPathEntity> pathList) async {
224 228
     if (pathList == null) {
225 229
       return;
226 230
     }
@@ -246,10 +250,6 @@ class _PhotoMainPageState extends State<PhotoMainPage>
246 250
     });
247 251
   }
248 252
 
249
-  void _sortAssetList(List<AssetEntity> assetList) {
250
-    options?.sortDelegate?.assetDelegate?.sort(assetList);
251
-  }
252
-
253 253
   Widget _buildBody() {
254 254
     if (!_isInit) {
255 255
       return _buildLoading();
@@ -394,7 +394,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
394 394
   }
395 395
 
396 396
   void _onItemClick(AssetEntity data, int index) {
397
-    var result = new PhotoPreviewResult();
397
+    var result = PhotoPreviewResult();
398 398
     isPushed = true;
399 399
     Navigator.of(context).push(
400 400
       MaterialPageRoute(
@@ -425,7 +425,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
425 425
   }
426 426
 
427 427
   void _onTapPreview() async {
428
-    var result = new PhotoPreviewResult();
428
+    var result = PhotoPreviewResult();
429 429
     isPushed = true;
430 430
     var v = await Navigator.of(context).push(
431 431
       MaterialPageRoute(