Skip to content

Commit e089538

Browse files
committed
✨ Add EntitySaveCallback for custom save method
1 parent babf7bf commit e089538

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Flutter SDK:`>=2.0.0` 。
7474
| cameraQuarterTurns | `int` | 摄像机视图顺时针旋转次数,每次90度 | `0` |
7575
| imageFormatGroup | `ImageFormatGroup` | 输出图像的格式描述 | `ImageFormatGroup.jpeg` |
7676
| foregroundBuilder | `Widget Function(CameraValue)?` | 覆盖在相机预览上方的前景构建 | null |
77+
| onEntitySaving | `SaveEntityCallback?` | 在查看器中保存图片时的回调 | null |
7778

7879
### 简单的使用方法
7980

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Flutter SDK: `>=2.0.0` .
9696
| cameraQuarterTurns | `int` | The number of clockwise quarter turns the camera view should be rotated. | `0` |
9797
| imageFormatGroup | `ImageFormatGroup` | Describes the output of the raw image format. | `ImageFormatGroup.jpeg` |
9898
| foregroundBuilder | `Widget Function(CameraValue)?` | The foreground widget builder which will cover the whole camera preview. | null |
99+
| onEntitySaving | `SaveEntityCallback?` | The callback type define for saving entity in the viewer. | null |
99100

100101
### Simple usage
101102

lib/src/widget/camera_picker.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class CameraPicker extends StatefulWidget {
4444
this.imageFormatGroup = ImageFormatGroup.jpeg,
4545
this.cameraQuarterTurns = 0,
4646
this.foregroundBuilder,
47+
this.onEntitySaving,
4748
CameraPickerTextDelegate? textDelegate,
4849
}) : assert(
4950
enableRecording == true || onlyEnableRecording != true,
@@ -122,6 +123,9 @@ class CameraPicker extends StatefulWidget {
122123
/// 覆盖在相机预览上方的前景构建
123124
final Widget Function(CameraValue)? foregroundBuilder;
124125

126+
/// {@macro wechat_camera_picker.SaveEntityCallback}
127+
final EntitySaveCallback? onEntitySaving;
128+
125129
/// Static method to create [AssetEntity] through camera.
126130
/// 通过相机创建 [AssetEntity] 的静态方法
127131
static Future<AssetEntity?> pickFromCamera(
@@ -141,6 +145,7 @@ class CameraPicker extends StatefulWidget {
141145
ResolutionPreset resolutionPreset = ResolutionPreset.max,
142146
ImageFormatGroup imageFormatGroup = ImageFormatGroup.jpeg,
143147
Widget Function(CameraValue)? foregroundBuilder,
148+
EntitySaveCallback? onEntitySaving,
144149
}) async {
145150
if (enableRecording != true && onlyEnableRecording == true) {
146151
throw ArgumentError('Recording mode error.');
@@ -166,6 +171,7 @@ class CameraPicker extends StatefulWidget {
166171
resolutionPreset: resolutionPreset,
167172
imageFormatGroup: imageFormatGroup,
168173
foregroundBuilder: foregroundBuilder,
174+
onEntitySaving: onEntitySaving,
169175
),
170176
transitionCurve: Curves.easeIn,
171177
transitionDuration: _kRouteDuration,
@@ -733,6 +739,7 @@ class CameraPickerState extends State<CameraPicker>
733739
previewXFile: await controller.takePicture(),
734740
theme: theme,
735741
shouldDeletePreviewFile: shouldDeletePreviewFile,
742+
onEntitySaving: widget.onEntitySaving,
736743
);
737744
if (entity != null) {
738745
Navigator.of(context).pop(entity);

lib/src/widget/camera_picker_viewer.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// [Author] Alex (https://github.com/AlexV525)
33
/// [Date] 2020/7/16 22:02
44
///
5+
import 'dart:async';
56
import 'dart:io';
67
import 'dart:typed_data';
78

@@ -18,6 +19,27 @@ import 'camera_picker.dart';
1819
/// 两种预览类型:图片和视频
1920
enum CameraPickerViewType { image, video }
2021

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+
2143
class CameraPickerViewer extends StatefulWidget {
2244
const CameraPickerViewer({
2345
Key? key,
@@ -26,6 +48,7 @@ class CameraPickerViewer extends StatefulWidget {
2648
required this.previewXFile,
2749
required this.theme,
2850
this.shouldDeletePreviewFile = false,
51+
this.onEntitySaving,
2952
}) : super(key: key);
3053

3154
/// State of the picker.
@@ -48,6 +71,9 @@ class CameraPickerViewer extends StatefulWidget {
4871
/// 返回页面时是否删除预览文件
4972
final bool shouldDeletePreviewFile;
5073

74+
/// {@macro wechat_camera_picker.SaveEntityCallback}
75+
final EntitySaveCallback? onEntitySaving;
76+
5177
/// Static method to push with the navigator.
5278
/// 跳转至选择预览的静态方法
5379
static Future<AssetEntity?> pushToViewer(
@@ -57,6 +83,7 @@ class CameraPickerViewer extends StatefulWidget {
5783
required XFile previewXFile,
5884
required ThemeData theme,
5985
bool shouldDeletePreviewFile = false,
86+
EntitySaveCallback? onEntitySaving,
6087
}) async {
6188
try {
6289
final Widget viewer = CameraPickerViewer(
@@ -65,6 +92,7 @@ class CameraPickerViewer extends StatefulWidget {
6592
previewXFile: previewXFile,
6693
theme: theme,
6794
shouldDeletePreviewFile: shouldDeletePreviewFile,
95+
onEntitySaving: onEntitySaving,
6896
);
6997
final PageRouteBuilder<AssetEntity?> pageRoute =
7098
PageRouteBuilder<AssetEntity?>(
@@ -201,6 +229,14 @@ class _CameraPickerViewerState extends State<CameraPickerViewer> {
201229
/// While the entity might returned null, there's no side effects if popping `null`
202230
/// because the parent picker will ignore it.
203231
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+
}
204240
try {
205241
Future<AssetEntity?> saveFuture;
206242

0 commit comments

Comments
 (0)