Skip to content

Commit b6c2327

Browse files
ZhuBoaoAlexV525
andauthored
✨ Introduce onXFileCaptured (#87)
Co-authored-by: Alex Li <[email protected]>
1 parent 16dc2cd commit b6c2327

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ final AssetEntity? entity = await CameraPicker.pickFromCamera(
118118
| previewTransformBuilder | `PreviewTransformBuilder?` | 对相机预览做变换的构建 | null |
119119
| onEntitySaving | `EntitySaveCallback?` | 在查看器中保存图片时的回调 | null |
120120
| onError | `CameraErrorHandler?` | 拍摄照片过程中的自定义错误处理 | null |
121+
| onXFileCaptured | `XFileCapturedCallback?` | 拍摄文件生成后的回调 | null |
121122

122123
## 常见问题 💭
123124

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Fields in `CameraPickerConfig`:
103103
| previewTransformBuilder | `PreviewTransformBuilder?` | The widget builder which will transform the camera preview. | null |
104104
| onEntitySaving | `EntitySaveCallback?` | The callback type define for saving entity in the viewer. | null |
105105
| onError | `CameraErrorHandler?` | The error handler when any error occurred during the picking process. | null |
106+
| onXFileCaptured | `XFileCapturedCallback?` | The callback type definition when the XFile is captured by the camera. | null |
106107

107108
## Frequently asked question 💭
108109

lib/src/constants/config.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CameraPickerConfig {
3737
this.previewTransformBuilder,
3838
this.onEntitySaving,
3939
this.onError,
40+
this.onXFileCaptured,
4041
}) : assert(
4142
enableRecording == true || onlyEnableRecording != true,
4243
'Recording mode error.',
@@ -137,4 +138,7 @@ class CameraPickerConfig {
137138

138139
/// {@macro wechat_camera_picker.CameraErrorHandler}
139140
final CameraErrorHandler? onError;
141+
142+
/// {@macro wechat_camera_picker.XFileCapturedCallback}
143+
final XFileCapturedCallback? onXFileCaptured;
140144
}

lib/src/constants/type_defs.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:async' show FutureOr;
66
import 'dart:io' show File;
77

8-
import 'package:camera/camera.dart' show CameraController, CameraValue;
8+
import 'package:camera/camera.dart' show CameraController, CameraValue, XFile;
99
import 'package:flutter/widgets.dart' show BuildContext, Widget;
1010

1111
import 'enums.dart';
@@ -58,3 +58,16 @@ typedef PreviewTransformBuilder = Widget? Function(
5858
CameraController controller,
5959
Widget child,
6060
);
61+
62+
/// {@template wechat_camera_picker.XFileCapturedCallback}
63+
/// The callback type definition when the XFile is captured by the camera.
64+
/// 拍摄文件生成后的回调
65+
///
66+
/// ### Notice about the implementation
67+
/// * After the callback is implemented, the default viewer page
68+
/// will not be pushed anymore.
69+
///
70+
/// ### 在实现时需要注意
71+
/// * 实现该方法后,默认的的预览页面不会再出现。
72+
/// {@endtemplate}
73+
typedef XFileCapturedCallback = void Function(XFile, CameraPickerViewType);

lib/src/widgets/camera_picker.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ class CameraPickerState extends State<CameraPicker>
654654
_controller = null;
655655
});
656656
});
657+
if (config.onXFileCaptured != null) {
658+
config.onXFileCaptured!(_file, CameraPickerViewType.image);
659+
return;
660+
}
657661
final AssetEntity? entity = await _pushToViewer(
658662
file: _file,
659663
viewType: CameraPickerViewType.image,
@@ -741,24 +745,29 @@ class CameraPickerState extends State<CameraPicker>
741745
}
742746

743747
if (controller.value.isRecordingVideo) {
744-
controller.stopVideoRecording().then((XFile file) async {
748+
try {
749+
final XFile file = await controller.stopVideoRecording();
750+
if (config.onXFileCaptured != null) {
751+
config.onXFileCaptured!(file, CameraPickerViewType.video);
752+
return;
753+
}
745754
final AssetEntity? entity = await _pushToViewer(
746755
file: file,
747756
viewType: CameraPickerViewType.video,
748757
);
749758
if (entity != null) {
750759
Navigator.of(context).pop(entity);
751760
}
752-
}).catchError((Object e) {
761+
} catch (e) {
753762
realDebugPrint('Error when stop recording video: $e');
754763
realDebugPrint('Try to initialize a new CameraController...');
755764
initCameras();
756765
_handleError();
757766
handleErrorWithHandler(e, config.onError);
758-
}).whenComplete(() {
767+
} finally {
759768
isShootingButtonAnimate = false;
760769
safeSetState(() {});
761-
});
770+
}
762771
return;
763772
}
764773
_handleError();
@@ -1275,7 +1284,9 @@ class CameraPickerState extends State<CameraPicker>
12751284
onScaleUpdate: enablePinchToZoom ? _handleScaleUpdate : null,
12761285
// Enabled cameras switching by default if we have multiple cameras.
12771286
onDoubleTap: cameras.length > 1 ? switchCameras : null,
1278-
child: CameraPreview(controller),
1287+
child: _controller != null
1288+
? CameraPreview(controller)
1289+
: const SizedBox.shrink(),
12791290
),
12801291
);
12811292

0 commit comments

Comments
 (0)