Explorar el Código

完善了预览页面的选择逻辑和大图显示逻辑

cjl_macbook hace 6 años
padre
commit
5affef481c
Se han modificado 4 ficheros con 56 adiciones y 27 borrados
  1. 9
    0
      lib/src/ui/page/photo_main_page.dart
  2. 44
    24
      lib/src/ui/page/photo_preview_page.dart
  3. 2
    2
      pubspec.lock
  4. 1
    1
      pubspec.yaml

+ 9
- 0
lib/src/ui/page/photo_main_page.dart Ver fichero

49
   GlobalKey scaffoldKey;
49
   GlobalKey scaffoldKey;
50
   ScrollController scrollController;
50
   ScrollController scrollController;
51
 
51
 
52
+  bool isPushed = false;
53
+
52
   @override
54
   @override
53
   void initState() {
55
   void initState() {
54
     super.initState();
56
     super.initState();
127
   }
129
   }
128
 
130
 
129
   void _showTip(String msg) {
131
   void _showTip(String msg) {
132
+    if (isPushed) {
133
+      return;
134
+    }
130
     Scaffold.of(scaffoldKey.currentContext).showSnackBar(
135
     Scaffold.of(scaffoldKey.currentContext).showSnackBar(
131
       SnackBar(
136
       SnackBar(
132
         content: Text(
137
         content: Text(
267
 
272
 
268
   void _onItemClick(ImageEntity data, int index) {
273
   void _onItemClick(ImageEntity data, int index) {
269
     var result = new PhotoPreviewResult();
274
     var result = new PhotoPreviewResult();
275
+    isPushed = true;
270
     Navigator.of(context).push(
276
     Navigator.of(context).push(
271
       MaterialPageRoute(
277
       MaterialPageRoute(
272
         builder: (ctx) {
278
         builder: (ctx) {
284
         },
290
         },
285
       ),
291
       ),
286
     ).then((v) {
292
     ).then((v) {
293
+      isPushed = false;
287
       setState(() {});
294
       setState(() {});
288
     });
295
     });
289
   }
296
   }
290
 
297
 
291
   void _onTapPreview() {
298
   void _onTapPreview() {
292
     var result = new PhotoPreviewResult();
299
     var result = new PhotoPreviewResult();
300
+    isPushed = true;
293
     Navigator.of(context)
301
     Navigator.of(context)
294
         .push(
302
         .push(
295
       MaterialPageRoute(
303
       MaterialPageRoute(
307
     )
315
     )
308
         .then(
316
         .then(
309
       (v) {
317
       (v) {
318
+        isPushed = false;
310
         compareAndRemoveEntities(result.previewSelectedList);
319
         compareAndRemoveEntities(result.previewSelectedList);
311
       },
320
       },
312
     );
321
     );

+ 44
- 24
lib/src/ui/page/photo_preview_page.dart Ver fichero

1
 import 'dart:async';
1
 import 'dart:async';
2
-import 'dart:io';
3
 import 'dart:typed_data';
2
 import 'dart:typed_data';
4
 
3
 
5
 import 'package:flutter/material.dart';
4
 import 'package:flutter/material.dart';
54
 
53
 
55
   bool get changeProviderOnCheckChange => widget.changeProviderOnCheckChange;
54
   bool get changeProviderOnCheckChange => widget.changeProviderOnCheckChange;
56
 
55
 
57
-  List<ImageEntity> get previewSelectedList =>
58
-      widget.result.previewSelectedList;
56
+  PhotoPreviewResult get result => widget.result;
57
+
58
+  /// 缩略图用的数据
59
+  ///
60
+  /// 用于与provider数据联动
61
+  List<ImageEntity> get previewList {
62
+    return selectedProvider.selectedList;
63
+  }
64
+
65
+  /// 选中的数据
66
+  List<ImageEntity> _selectedList = [];
67
+
68
+  List<ImageEntity> get selectedList {
69
+    if (changeProviderOnCheckChange) {
70
+      return previewList;
71
+    }
72
+    return _selectedList;
73
+  }
59
 
74
 
60
   PageController pageController;
75
   PageController pageController;
61
 
76
 
63
   void initState() {
78
   void initState() {
64
     super.initState();
79
     super.initState();
65
     pageChangeController.add(0);
80
     pageChangeController.add(0);
66
-    previewSelectedList.clear();
67
-    previewSelectedList.addAll(selectedProvider.selectedList);
68
     pageController = PageController(
81
     pageController = PageController(
69
       initialPage: widget.initIndex,
82
       initialPage: widget.initIndex,
70
     );
83
     );
84
+
85
+    _selectedList.clear();
86
+    _selectedList.addAll(selectedProvider.selectedList);
87
+
88
+    result.previewSelectedList = _selectedList;
71
   }
89
   }
72
 
90
 
73
   @override
91
   @override
128
           var index = snapshot.data;
146
           var index = snapshot.data;
129
           var data = list[index];
147
           var data = list[index];
130
           return CheckboxListTile(
148
           return CheckboxListTile(
131
-            value: previewSelectedList.contains(data),
149
+            value: selectedList.contains(data),
132
             onChanged: (bool check) {
150
             onChanged: (bool check) {
133
               if (changeProviderOnCheckChange) {
151
               if (changeProviderOnCheckChange) {
134
                 _onChangeProvider(check, index);
152
                 _onChangeProvider(check, index);
154
   void _onCheckInOnlyPreview(bool check, int index) {
172
   void _onCheckInOnlyPreview(bool check, int index) {
155
     var item = list[index];
173
     var item = list[index];
156
     if (check) {
174
     if (check) {
157
-      previewSelectedList.add(item);
175
+      selectedList.add(item);
158
     } else {
176
     } else {
159
-      previewSelectedList.remove(item);
177
+      selectedList.remove(item);
160
     }
178
     }
161
     pageChangeController.add(index);
179
     pageChangeController.add(index);
162
   }
180
   }
165
   void _onChangeProvider(bool check, int index) {
183
   void _onChangeProvider(bool check, int index) {
166
     var item = list[index];
184
     var item = list[index];
167
     if (check) {
185
     if (check) {
168
-      if (selectedProvider.addSelectEntity(item)) {
169
-        previewSelectedList.add(item);
170
-      }
186
+      selectedProvider.addSelectEntity(item);
171
     } else {
187
     } else {
172
       selectedProvider.removeSelectEntity(item);
188
       selectedProvider.removeSelectEntity(item);
173
-      previewSelectedList.remove(item);
174
     }
189
     }
175
     pageChangeController.add(index);
190
     pageChangeController.add(index);
176
   }
191
   }
187
   }
202
   }
188
 
203
 
189
   Widget _buildThumb() {
204
   Widget _buildThumb() {
190
-    return Container(
191
-      height: 80.0,
192
-      child: ListView.builder(
193
-        itemBuilder: _buildThumbItem,
194
-        itemCount: previewSelectedList.length,
195
-        scrollDirection: Axis.horizontal,
196
-      ),
205
+    return StreamBuilder(
206
+      builder: (ctx, snapshot) => Container(
207
+            height: 80.0,
208
+            child: ListView.builder(
209
+              itemBuilder: _buildThumbItem,
210
+              itemCount: previewList.length,
211
+              scrollDirection: Axis.horizontal,
212
+            ),
213
+          ),
214
+      stream: pageStream,
197
     );
215
     );
198
   }
216
   }
199
 
217
 
200
   Widget _buildThumbItem(BuildContext context, int index) {
218
   Widget _buildThumbItem(BuildContext context, int index) {
201
-    var item = previewSelectedList[index];
219
+    var item = previewList[index];
202
     return GestureDetector(
220
     return GestureDetector(
203
       onTap: () => changeSelected(item, index),
221
       onTap: () => changeSelected(item, index),
204
       child: Container(
222
       child: Container(
213
               child: StreamBuilder(
231
               child: StreamBuilder(
214
                 stream: pageStream,
232
                 stream: pageStream,
215
                 builder: (BuildContext context, AsyncSnapshot snapshot) {
233
                 builder: (BuildContext context, AsyncSnapshot snapshot) {
216
-                  if (previewSelectedList.contains(item)) {
234
+                  if (selectedList.contains(item)) {
217
                     return Container();
235
                     return Container();
218
                   }
236
                   }
219
                   return Container(
237
                   return Container(
247
     with AutomaticKeepAliveClientMixin {
265
     with AutomaticKeepAliveClientMixin {
248
   @override
266
   @override
249
   Widget build(BuildContext context) {
267
   Widget build(BuildContext context) {
268
+    var width = MediaQuery.of(context).size.width;
269
+    var height = MediaQuery.of(context).size.height;
250
     return FutureBuilder(
270
     return FutureBuilder(
251
-      future: widget.imageEntity.thumbDataWithSize(1300, 1300),
271
+      future:
272
+          widget.imageEntity.thumbDataWithSize(width.floor(), height.floor()),
252
       builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
273
       builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
253
         var file = snapshot.data;
274
         var file = snapshot.data;
254
         if (snapshot.connectionState == ConnectionState.done && file != null) {
275
         if (snapshot.connectionState == ConnectionState.done && file != null) {
255
-          // todo ios 图片优化,在返回时再生成图片
256
-          // 展示时,不允许放大,使用原生方案生成一个与屏幕同宽的图片
276
+          print(file.length);
257
           return Image.memory(
277
           return Image.memory(
258
             file,
278
             file,
259
             fit: BoxFit.contain,
279
             fit: BoxFit.contain,

+ 2
- 2
pubspec.lock Ver fichero

211
     dependency: "direct main"
211
     dependency: "direct main"
212
     description:
212
     description:
213
       path: "."
213
       path: "."
214
-      ref: "6da7b897b0c0a174a4285831193428c8c0df73d5"
215
-      resolved-ref: "6da7b897b0c0a174a4285831193428c8c0df73d5"
214
+      ref: c87060911e1312113a9df43bec084e0c031ac58a
215
+      resolved-ref: c87060911e1312113a9df43bec084e0c031ac58a
216
       url: "https://github.com/CaiJingLong/flutter_photo_manager.git"
216
       url: "https://github.com/CaiJingLong/flutter_photo_manager.git"
217
     source: git
217
     source: git
218
     version: "0.0.1"
218
     version: "0.0.1"

+ 1
- 1
pubspec.yaml Ver fichero

13
   photo_manager:
13
   photo_manager:
14
     git:
14
     git:
15
       url: https://github.com/CaiJingLong/flutter_photo_manager.git
15
       url: https://github.com/CaiJingLong/flutter_photo_manager.git
16
-      ref: 6da7b897b0c0a174a4285831193428c8c0df73d5
16
+      ref: c87060911e1312113a9df43bec084e0c031ac58a
17
 #  photo_manager:
17
 #  photo_manager:
18
 #    path: /Users/caijinglong/code/flutter/plugin/flutter_photo_manager/
18
 #    path: /Users/caijinglong/code/flutter/plugin/flutter_photo_manager/
19
 
19