Sfoglia il codice sorgente

update preview to preview pick asset

Caijinglong 5 anni fa
parent
commit
c944b6ae9f
3 ha cambiato i file con 98 aggiunte e 0 eliminazioni
  1. 64
    0
      example/lib/asset_image.dart
  2. 6
    0
      example/lib/main.dart
  3. 28
    0
      example/lib/preview.dart

+ 64
- 0
example/lib/asset_image.dart Vedi File

@@ -0,0 +1,64 @@
1
+import 'dart:typed_data';
2
+
3
+import 'package:flutter/material.dart';
4
+import 'package:photo_manager/photo_manager.dart';
5
+
6
+class AssetImageWidget extends StatelessWidget {
7
+  final AssetEntity assetEntity;
8
+  final double width;
9
+  final double height;
10
+  final BoxFit boxFit;
11
+
12
+  const AssetImageWidget({
13
+    Key key,
14
+    @required this.assetEntity,
15
+    this.width,
16
+    this.height,
17
+    this.boxFit,
18
+  }) : super(key: key);
19
+
20
+  @override
21
+  Widget build(BuildContext context) {
22
+    if (assetEntity == null) {
23
+      return _buildContainer();
24
+    }
25
+    return FutureBuilder<Size>(
26
+      builder: (c, s) {
27
+        if (!s.hasData) {
28
+          return Container();
29
+        }
30
+        var size = s.data;
31
+        return FutureBuilder<Uint8List>(
32
+          builder: (BuildContext context, snapshot) {
33
+            if (snapshot.hasData) {
34
+              return _buildContainer(
35
+                child: Image.memory(
36
+                  snapshot.data,
37
+                  width: width,
38
+                  height: height,
39
+                  fit: boxFit,
40
+                ),
41
+              );
42
+            } else {
43
+              return _buildContainer();
44
+            }
45
+          },
46
+          future: assetEntity.thumbDataWithSize(
47
+            size.width.toInt(),
48
+            size.height.toInt(),
49
+          ),
50
+        );
51
+      },
52
+      future: assetEntity.size,
53
+    );
54
+  }
55
+
56
+  Widget _buildContainer({Widget child}) {
57
+    child ??= Container();
58
+    return Container(
59
+      width: width,
60
+      height: height,
61
+      child: child,
62
+    );
63
+  }
64
+}

+ 6
- 0
example/lib/main.dart Vedi File

@@ -1,3 +1,4 @@
1
+import 'package:example/preview.dart';
1 2
 import 'package:flutter/cupertino.dart';
2 3
 import 'package:flutter/material.dart';
3 4
 import 'package:photo/photo.dart';
@@ -162,6 +163,11 @@ class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
162 163
         r.add(file.absolute.path);
163 164
       }
164 165
       currentSelected = r.join("\n\n");
166
+
167
+      List<AssetEntity> preview = [];
168
+      preview.addAll(imgList);
169
+      Navigator.push(context,
170
+          MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
165 171
     }
166 172
     setState(() {});
167 173
   }

+ 28
- 0
example/lib/preview.dart Vedi File

@@ -0,0 +1,28 @@
1
+import 'package:example/asset_image.dart';
2
+import 'package:flutter/material.dart';
3
+import 'package:photo_manager/photo_manager.dart';
4
+
5
+class PreviewPage extends StatelessWidget {
6
+  final List<AssetEntity> list;
7
+
8
+  const PreviewPage({Key key, this.list = const []}) : super(key: key);
9
+
10
+  @override
11
+  Widget build(BuildContext context) {
12
+    return Scaffold(
13
+      appBar: AppBar(
14
+        title: Text("预览"),
15
+      ),
16
+      body: ListView(
17
+        children: list
18
+            .map((item) => AssetImageWidget(
19
+                  assetEntity: item,
20
+                  width: 300,
21
+                  height: 200,
22
+                  boxFit: BoxFit.cover,
23
+                ))
24
+            .toList(),
25
+      ),
26
+    );
27
+  }
28
+}