Quellcode durchsuchen

Update picked asset for list.

Caijinglong vor 4 Jahren
Ursprung
Commit
3509ac0532

+ 23
- 0
example/lib/icon_text_button.dart Datei anzeigen

@@ -0,0 +1,23 @@
1
+import 'package:flutter/material.dart';
2
+
3
+class IconTextButton extends StatelessWidget {
4
+  final IconData icon;
5
+  final String text;
6
+  final Function onTap;
7
+
8
+  const IconTextButton({Key key, this.icon, this.text, this.onTap})
9
+      : super(key: key);
10
+
11
+  @override
12
+  Widget build(BuildContext context) {
13
+    return InkWell(
14
+      onTap: onTap,
15
+      child: Container(
16
+        child: ListTile(
17
+          leading: Icon(icon ?? Icons.device_unknown),
18
+          title: Text(text ?? ""),
19
+        ),
20
+      ),
21
+    );
22
+  }
23
+}

+ 26
- 31
example/lib/main.dart Datei anzeigen

@@ -5,6 +5,8 @@ import 'package:photo/photo.dart';
5 5
 import 'package:photo_manager/photo_manager.dart';
6 6
 
7 7
 import './preview.dart';
8
+import 'icon_text_button.dart';
9
+import 'picked_example.dart';
8 10
 
9 11
 void main() => runApp(MyApp());
10 12
 
@@ -16,7 +18,7 @@ class MyApp extends StatelessWidget {
16 18
       child: MaterialApp(
17 19
         title: 'Pick Image Demo',
18 20
         theme: ThemeData(
19
-          primarySwatch: Colors.blue,
21
+          primarySwatch: Colors.lime,
20 22
         ),
21 23
         home: MyHomePage(title: 'Pick Image Demo'),
22 24
       ),
@@ -80,20 +82,24 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
80 82
           child: Column(
81 83
             children: <Widget>[
82 84
               IconTextButton(
83
-                  icon: Icons.photo,
84
-                  text: "photo",
85
-                  onTap: () => _pickAsset(PickType.onlyImage)),
85
+                icon: Icons.photo,
86
+                text: "photo",
87
+                onTap: () => _pickAsset(PickType.onlyImage),
88
+              ),
89
+              IconTextButton(
90
+                icon: Icons.videocam,
91
+                text: "video",
92
+                onTap: () => _pickAsset(PickType.onlyVideo),
93
+              ),
86 94
               IconTextButton(
87
-                  icon: Icons.videocam,
88
-                  text: "video",
89
-                  onTap: () => _pickAsset(PickType.onlyVideo)),
95
+                icon: Icons.album,
96
+                text: "all",
97
+                onTap: () => _pickAsset(PickType.all),
98
+              ),
90 99
               IconTextButton(
91
-                  icon: Icons.album,
92
-                  text: "all",
93
-                  onTap: () => _pickAsset(PickType.all)),
94
-              Text(
95
-                '$currentSelected',
96
-                textAlign: TextAlign.center,
100
+                icon: CupertinoIcons.reply_all,
101
+                text: "Picked asset example.",
102
+                onTap: () => routePage(PickedExample()),
97 103
               ),
98 104
             ],
99 105
           ),
@@ -182,25 +188,14 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
182 188
     }
183 189
     setState(() {});
184 190
   }
185
-}
186
-
187
-class IconTextButton extends StatelessWidget {
188
-  final IconData icon;
189
-  final String text;
190
-  final Function onTap;
191 191
 
192
-  const IconTextButton({Key key, this.icon, this.text, this.onTap})
193
-      : super(key: key);
194
-
195
-  @override
196
-  Widget build(BuildContext context) {
197
-    return InkWell(
198
-      onTap: onTap,
199
-      child: Container(
200
-        child: ListTile(
201
-          leading: Icon(icon ?? Icons.device_unknown),
202
-          title: Text(text ?? ""),
203
-        ),
192
+  void routePage(Widget widget) {
193
+    Navigator.push(
194
+      context,
195
+      MaterialPageRoute(
196
+        builder: (BuildContext context) {
197
+          return widget;
198
+        },
204 199
       ),
205 200
     );
206 201
   }

+ 48
- 0
example/lib/picked_example.dart Datei anzeigen

@@ -0,0 +1,48 @@
1
+import 'package:flutter/cupertino.dart';
2
+import 'package:flutter/material.dart';
3
+import 'package:photo/photo.dart';
4
+
5
+import 'icon_text_button.dart';
6
+import 'package:photo_manager/photo_manager.dart';
7
+
8
+class PickedExample extends StatefulWidget {
9
+  @override
10
+  _PickedExampleState createState() => _PickedExampleState();
11
+}
12
+
13
+class _PickedExampleState extends State<PickedExample> {
14
+  List<AssetEntity> picked = [];
15
+
16
+  @override
17
+  Widget build(BuildContext context) {
18
+    return Scaffold(
19
+      appBar: AppBar(
20
+        title: Text("Cross Picked asset"),
21
+      ),
22
+      body: Column(
23
+        children: <Widget>[
24
+          IconTextButton(
25
+            icon: Icons.assignment,
26
+            text: "Pick asset",
27
+            onTap: _pickAsset,
28
+          ),
29
+          ListTile(
30
+            title: Text("picked asset count = ${picked.length}"),
31
+          ),
32
+        ],
33
+      ),
34
+    );
35
+  }
36
+
37
+  void _pickAsset() async {
38
+    final result = await PhotoPicker.pickAsset(
39
+      context: context,
40
+      pickedAssetList: picked,
41
+    );
42
+    if (result != null && result.isNotEmpty) {
43
+      picked.clear();
44
+      picked.addAll(result);
45
+      setState(() {});
46
+    }
47
+  }
48
+}

+ 0
- 2
lib/src/provider/asset_provider.dart Datei anzeigen

@@ -31,8 +31,6 @@ class AssetProvider {
31 31
   bool get noMore => getPaging()?.noMore ?? false;
32 32
 
33 33
   int get count => data?.length ?? 0;
34
-
35
-  void addPickedAssetList() {}
36 34
 }
37 35
 
38 36
 class AssetPaging {

+ 2
- 4
lib/src/provider/config_provider.dart Datei anzeigen

@@ -8,10 +8,12 @@ class PhotoPickerProvider extends InheritedWidget {
8 8
   final Options options;
9 9
   final I18nProvider provider;
10 10
   final AssetProvider assetProvider = AssetProvider();
11
+  final List<AssetEntity> pickedAssetList;
11 12
   PhotoPickerProvider({
12 13
     @required this.options,
13 14
     @required this.provider,
14 15
     @required Widget child,
16
+    this.pickedAssetList,
15 17
     Key key,
16 18
   }) : super(key: key, child: child);
17 19
 
@@ -25,8 +27,4 @@ class PhotoPickerProvider extends InheritedWidget {
25 27
 
26 28
   static AssetProvider assetProviderOf(BuildContext context) =>
27 29
       of(context).assetProvider;
28
-
29
-  void addPickedAssetList(List<AssetEntity> pickedAssetList) {
30
-    assetProvider.addPickedAssetList();
31
-  }
32 30
 }

+ 6
- 0
lib/src/provider/selected_provider.dart Datei anzeigen

@@ -57,4 +57,10 @@ abstract class SelectedProvider {
57 57
       return notExistsList.contains(e);
58 58
     });
59 59
   }
60
+
61
+  addPickedAsset(List<AssetEntity> list) {
62
+    for (final entity in list) {
63
+      addSelectEntity(entity);
64
+    }
65
+  }
60 66
 }

+ 1
- 0
lib/src/ui/page/photo_main_page.dart Datei anzeigen

@@ -92,6 +92,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
92 92
   void didChangeDependencies() {
93 93
     super.didChangeDependencies();
94 94
     if (!_isInit) {
95
+      addPickedAsset(PhotoPickerProvider.of(context).pickedAssetList.toList());
95 96
       _refreshList();
96 97
     }
97 98
   }

+ 1
- 0
lib/src/ui/photo_app.dart Datei anzeigen

@@ -25,6 +25,7 @@ class PhotoApp extends StatelessWidget {
25 25
     final pickerProvider = PhotoPickerProvider(
26 26
       provider: provider,
27 27
       options: options,
28
+      pickedAssetList: pickedAssetList,
28 29
       child: PhotoMainPage(
29 30
         onClose: (List<AssetEntity> value) {
30 31
           Navigator.pop(context, value);