Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Add custom onPicked callback when asset entity is generated #268

Merged
merged 5 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ final AssetEntity? entity = await CameraPicker.pickFromCamera(
| onError | `CameraErrorHandler?` | 拍摄照片过程中的自定义错误处理 | null |
| onXFileCaptured | `XFileCapturedCallback?` | 拍摄文件生成后的回调 | null |
| onMinimumRecordDurationNotMet | `VoidCallback?` | 录制时长未达到最小时长时的回调方法 | null |
| onPickConfirmed | `void Function(AssetEntity)?` | 拍照或录像确认时的回调方法。 | null |

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

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

### Using custom `State`s

Expand Down
6 changes: 6 additions & 0 deletions lib/src/constants/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:photo_manager/photo_manager.dart';

import '../delegates/camera_picker_text_delegate.dart';
import 'type_defs.dart';
Expand Down Expand Up @@ -42,6 +43,7 @@ final class CameraPickerConfig {
this.onError,
this.onXFileCaptured,
this.onMinimumRecordDurationNotMet,
this.onPickConfirmed,
}) : assert(
enableRecording == true || onlyEnableRecording != true,
'Recording mode error.',
Expand Down Expand Up @@ -165,4 +167,8 @@ final class CameraPickerConfig {
/// The callback when the recording is not met the minimum recording duration.
/// 录制时长未达到最小时长时的回调方法。
final VoidCallback? onMinimumRecordDurationNotMet;

/// The callback when the picture or the video is confirmed as picked.
/// 拍照或录像确认时的回调方法。
final void Function(AssetEntity)? onPickConfirmed;
}
16 changes: 12 additions & 4 deletions lib/src/states/camera_picker_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import 'package:sensors_plus/sensors_plus.dart';
import 'package:wechat_picker_library/wechat_picker_library.dart';

import '../constants/config.dart';
import '../internals/singleton.dart';
import '../constants/enums.dart';
import '../delegates/camera_picker_text_delegate.dart';
import '../internals/methods.dart';
import '../internals/singleton.dart';
import '../widgets/camera_focus_point.dart';
import '../widgets/camera_picker.dart';
import '../widgets/camera_picker_viewer.dart';
Expand Down Expand Up @@ -926,8 +926,11 @@ class CameraPickerState extends State<CameraPicker>
viewType: CameraPickerViewType.image,
);
if (entity != null) {
Navigator.of(context).pop(entity);
return;
if (pickerConfig.onPickConfirmed case final onPickConfirmed?) {
onPickConfirmed(entity);
} else {
return Navigator.of(context).pop(entity);
}
}
wrapControllerMethod<void>(
'setFocusMode',
Expand Down Expand Up @@ -1056,7 +1059,12 @@ class CameraPickerState extends State<CameraPicker>
viewType: CameraPickerViewType.video,
);
if (entity != null) {
Navigator.of(context).pop(entity);
if (pickerConfig.onPickConfirmed case final onPickConfirmed?) {
await controller.resumePreview();
onPickConfirmed(entity);
} else {
Navigator.of(context).pop(entity);
}
} else {
await controller.resumePreview();
}
Expand Down
Loading