Browse Source

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

Fix: refresh from assetPathList.
Caijinglong 5 years ago
parent
commit
347423e765
No account linked to committer's email address

+ 2
- 1
example/android/gradle.properties View File

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

+ 17
- 14
example/lib/main.dart View File

1
 import 'package:flutter/cupertino.dart';
1
 import 'package:flutter/cupertino.dart';
2
 import 'package:flutter/material.dart';
2
 import 'package:flutter/material.dart';
3
+import 'package:oktoast/oktoast.dart';
3
 import 'package:photo/photo.dart';
4
 import 'package:photo/photo.dart';
4
 import 'package:photo_manager/photo_manager.dart';
5
 import 'package:photo_manager/photo_manager.dart';
5
 
6
 
6
 import './preview.dart';
7
 import './preview.dart';
7
 
8
 
8
-void main() => runApp(new MyApp());
9
+void main() => runApp(MyApp());
9
 
10
 
10
 class MyApp extends StatelessWidget {
11
 class MyApp extends StatelessWidget {
11
   // This widget is the root of your application.
12
   // This widget is the root of your application.
12
   @override
13
   @override
13
   Widget build(BuildContext context) {
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
   final String title;
29
   final String title;
27
 
30
 
28
   @override
31
   @override
29
-  _MyHomePageState createState() => new _MyHomePageState();
32
+  _MyHomePageState createState() => _MyHomePageState();
30
 }
33
 }
31
 
34
 
32
 class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
35
 class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
62
 
65
 
63
   @override
66
   @override
64
   Widget build(BuildContext context) {
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
         actions: <Widget>[
71
         actions: <Widget>[
69
           FlatButton(
72
           FlatButton(
70
             child: Icon(Icons.image),
73
             child: Icon(Icons.image),
96
           ),
99
           ),
97
         ),
100
         ),
98
       ),
101
       ),
99
-      floatingActionButton: new FloatingActionButton(
102
+      floatingActionButton: FloatingActionButton(
100
         onPressed: () => _pickAsset(PickType.all),
103
         onPressed: () => _pickAsset(PickType.all),
101
         tooltip: 'pickImage',
104
         tooltip: 'pickImage',
102
-        child: new Icon(Icons.add),
105
+        child: Icon(Icons.add),
103
       ),
106
       ),
104
     );
107
     );
105
   }
108
   }
158
       photoPathList: pathList,
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
     } else {
166
     } else {
164
       List<String> r = [];
167
       List<String> r = [];
165
       for (var e in imgList) {
168
       for (var e in imgList) {

+ 2
- 6
example/pubspec.yaml View File

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

+ 1
- 1
example/test/widget_test.dart View File

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

+ 1
- 1
lib/src/engine/lru_cache.dart View File

40
 typedef EvictionHandler<K, V>(K key, V value);
40
 typedef EvictionHandler<K, V>(K key, V value);
41
 
41
 
42
 class LRUMap<K, V> {
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
   final int _maxSize;
44
   final int _maxSize;
45
   final EvictionHandler<K, V> _handler;
45
   final EvictionHandler<K, V> _handler;
46
 
46
 

+ 17
- 17
lib/src/ui/page/photo_main_page.dart View File

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