Skip to content

Commit 77f32bc

Browse files
yujuneyujuneteeAlexV525
authored
✨Add custom onPicked callback when asset entity is generated (#268)
### Context Currently, when a picture or video is taken using the camera, the AssetEntity is retrieved via: ``` final assetEntity = await pickFromCamera(context); ``` This is achieved by calling `Navigator.of(context).pop(entity);`, which returns the `AssetEntity` to the previous screen. However, this approach has a limitation when the `CameraPicker` widget is used in a scenario where it doesn't trigger a pop event, such as when the widget is part of a tab or a persistent page in a navigation stack. In these cases, there is no straightforward way to retrieve the captured `AssetEntity`. ### Changes - Introduced a custom `onPicked` callback that directly provides the generated AssetEntity without relying on the `Navigator.of(context).pop(entity);`. --------- Co-authored-by: yujune <[email protected]> Co-authored-by: Alex Li <[email protected]>
1 parent 91b2ab6 commit 77f32bc

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ final AssetEntity? entity = await CameraPicker.pickFromCamera(
206206
| onError | `CameraErrorHandler?` | 拍摄照片过程中的自定义错误处理 | null |
207207
| onXFileCaptured | `XFileCapturedCallback?` | 拍摄文件生成后的回调 | null |
208208
| onMinimumRecordDurationNotMet | `VoidCallback?` | 录制时长未达到最小时长时的回调方法 | null |
209+
| onPickConfirmed | `void Function(AssetEntity)?` | 拍照或录像确认时的回调方法。 | null |
209210

210211
### 使用自定义的 `State`
211212

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ Fields in `CameraPickerConfig`:
211211
| onError | `CameraErrorHandler?` | The error handler when any error occurred during the picking process. | null |
212212
| onXFileCaptured | `XFileCapturedCallback?` | The callback type definition when the XFile is captured by the camera. | null |
213213
| onMinimumRecordDurationNotMet | `VoidCallback?` | The callback when the recording is not met the minimum recording duration. | null |
214+
| onPickConfirmed | `void Function(AssetEntity)?` | The callback when picture is taken or video is confirmed. | null |
214215

215216
### Using custom `State`s
216217

lib/src/constants/config.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:camera/camera.dart';
66
import 'package:flutter/material.dart';
77
import 'package:flutter/services.dart';
8+
import 'package:photo_manager/photo_manager.dart';
89

910
import '../delegates/camera_picker_text_delegate.dart';
1011
import 'type_defs.dart';
@@ -42,6 +43,7 @@ final class CameraPickerConfig {
4243
this.onError,
4344
this.onXFileCaptured,
4445
this.onMinimumRecordDurationNotMet,
46+
this.onPickConfirmed,
4547
}) : assert(
4648
enableRecording == true || onlyEnableRecording != true,
4749
'Recording mode error.',
@@ -165,4 +167,8 @@ final class CameraPickerConfig {
165167
/// The callback when the recording is not met the minimum recording duration.
166168
/// 录制时长未达到最小时长时的回调方法。
167169
final VoidCallback? onMinimumRecordDurationNotMet;
170+
171+
/// The callback when the picture or the video is confirmed as picked.
172+
/// 拍照或录像确认时的回调方法。
173+
final void Function(AssetEntity)? onPickConfirmed;
168174
}

lib/src/states/camera_picker_state.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import 'package:sensors_plus/sensors_plus.dart';
1818
import 'package:wechat_picker_library/wechat_picker_library.dart';
1919

2020
import '../constants/config.dart';
21-
import '../internals/singleton.dart';
2221
import '../constants/enums.dart';
2322
import '../delegates/camera_picker_text_delegate.dart';
2423
import '../internals/methods.dart';
24+
import '../internals/singleton.dart';
2525
import '../widgets/camera_focus_point.dart';
2626
import '../widgets/camera_picker.dart';
2727
import '../widgets/camera_picker_viewer.dart';
@@ -936,8 +936,11 @@ class CameraPickerState extends State<CameraPicker>
936936
viewType: CameraPickerViewType.image,
937937
);
938938
if (entity != null) {
939-
Navigator.of(context).pop(entity);
940-
return;
939+
if (pickerConfig.onPickConfirmed case final onPickConfirmed?) {
940+
onPickConfirmed(entity);
941+
} else {
942+
return Navigator.of(context).pop(entity);
943+
}
941944
}
942945
wrapControllerMethod<void>(
943946
'setFocusMode',
@@ -1066,7 +1069,12 @@ class CameraPickerState extends State<CameraPicker>
10661069
viewType: CameraPickerViewType.video,
10671070
);
10681071
if (entity != null) {
1069-
Navigator.of(context).pop(entity);
1072+
if (pickerConfig.onPickConfirmed case final onPickConfirmed?) {
1073+
await controller.resumePreview();
1074+
onPickConfirmed(entity);
1075+
} else {
1076+
Navigator.of(context).pop(entity);
1077+
}
10701078
} else {
10711079
await controller.resumePreview();
10721080
}

0 commit comments

Comments
 (0)