Skip to content

Commit

Permalink
✨Add custom onPicked callback when asset entity is generated (#268)
Browse files Browse the repository at this point in the history
### 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 <yujune@feedme.cc>
Co-authored-by: Alex Li <github@alexv525.com>
  • Loading branch information
3 people authored Sep 21, 2024
1 parent 91b2ab6 commit 77f32bc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
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 @@ -18,10 +18,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 @@ -936,8 +936,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 @@ -1066,7 +1069,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

0 comments on commit 77f32bc

Please sign in to comment.