2
2
/// [Author] Alex (https://github.com/AlexV525)
3
3
/// [Date] 2020/7/16 22:02
4
4
///
5
+ import 'dart:async' ;
5
6
import 'dart:io' ;
6
7
import 'dart:typed_data' ;
7
8
@@ -18,6 +19,27 @@ import 'camera_picker.dart';
18
19
/// 两种预览类型:图片和视频
19
20
enum CameraPickerViewType { image, video }
20
21
22
+ /// {@template wechat_camera_picker.SaveEntityCallback}
23
+ /// The callback type define for saving entity in the viewer.
24
+ /// 在查看器中保存图片时的回调
25
+ ///
26
+ /// ### Notice about the implementation
27
+ /// * After the callback is implemented, the default saving method
28
+ /// won't called anymore.
29
+ /// * Don't call `Navigator.of(context).pop/maybePop` without popping `null` or
30
+ /// `AssetEntity`, otherwise there will be a type cast error occurred.
31
+ ///
32
+ /// ### 在实现时需要注意
33
+ /// * 实现该方法后,原本的保存方法不会再被调用;
34
+ /// * 不要使用 `Navigator.of(context).pop/maybePop` 返回 `null` 或 `AssetEntity`
35
+ /// 以外类型的内容,否则会抛出类型转换异常。
36
+ /// {@endtemplate}
37
+ typedef EntitySaveCallback = FutureOr <dynamic > Function ({
38
+ BuildContext context,
39
+ CameraPickerViewType viewType,
40
+ File file,
41
+ });
42
+
21
43
class CameraPickerViewer extends StatefulWidget {
22
44
const CameraPickerViewer ({
23
45
Key ? key,
@@ -26,6 +48,7 @@ class CameraPickerViewer extends StatefulWidget {
26
48
required this .previewXFile,
27
49
required this .theme,
28
50
this .shouldDeletePreviewFile = false ,
51
+ this .onEntitySaving,
29
52
}) : super (key: key);
30
53
31
54
/// State of the picker.
@@ -48,6 +71,9 @@ class CameraPickerViewer extends StatefulWidget {
48
71
/// 返回页面时是否删除预览文件
49
72
final bool shouldDeletePreviewFile;
50
73
74
+ /// {@macro wechat_camera_picker.SaveEntityCallback}
75
+ final EntitySaveCallback ? onEntitySaving;
76
+
51
77
/// Static method to push with the navigator.
52
78
/// 跳转至选择预览的静态方法
53
79
static Future <AssetEntity ?> pushToViewer (
@@ -57,6 +83,7 @@ class CameraPickerViewer extends StatefulWidget {
57
83
required XFile previewXFile,
58
84
required ThemeData theme,
59
85
bool shouldDeletePreviewFile = false ,
86
+ EntitySaveCallback ? onEntitySaving,
60
87
}) async {
61
88
try {
62
89
final Widget viewer = CameraPickerViewer (
@@ -65,6 +92,7 @@ class CameraPickerViewer extends StatefulWidget {
65
92
previewXFile: previewXFile,
66
93
theme: theme,
67
94
shouldDeletePreviewFile: shouldDeletePreviewFile,
95
+ onEntitySaving: onEntitySaving,
68
96
);
69
97
final PageRouteBuilder <AssetEntity ?> pageRoute =
70
98
PageRouteBuilder <AssetEntity ?>(
@@ -201,6 +229,14 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
201
229
/// While the entity might returned null, there's no side effects if popping `null`
202
230
/// because the parent picker will ignore it.
203
231
Future <void > createAssetEntityAndPop () async {
232
+ if (widget.onEntitySaving != null ) {
233
+ await widget.onEntitySaving !(
234
+ context: context,
235
+ viewType: pickerType,
236
+ file: previewFile,
237
+ );
238
+ return ;
239
+ }
204
240
try {
205
241
Future <AssetEntity ?> saveFuture;
206
242
0 commit comments