Skip to content

Commit 3a704c3

Browse files
committed
♻️ Abstract CameraPickerConfig (#78)
1 parent d935c56 commit 3a704c3

File tree

8 files changed

+250
-283
lines changed

8 files changed

+250
-283
lines changed

README-ZH.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Language: [English](README.md) | 中文简体
2323
- [准备工作 🍭](#准备工作-)
2424
- [使用方法 📖](#使用方法-)
2525
- [简单的使用方法](#简单的使用方法)
26+
- [使用配置](#使用配置)
2627
- [常见问题 💭](#常见问题-)
2728
- [iOS 上的预览在旋转时行为诡异](#iOS-上的预览在旋转时行为诡异)
2829

@@ -57,6 +58,42 @@ Flutter SDK:`>=2.0.0` 。
5758

5859
## 使用方法 📖
5960

61+
### 简单的使用方法
62+
63+
```dart
64+
final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
65+
```
66+
67+
### 使用配置
68+
69+
你可以使用 `CameraPickerConfig` 来调整选择时的行为。
70+
71+
```dart
72+
final AssetEntity? entity = await CameraPicker.pickFromCamera(
73+
context,
74+
pickerConfig: const CameraPickerConfig(),
75+
);
76+
```
77+
78+
`CameraPickerConfig` 的成员说明:
79+
80+
### 简单的使用方法
81+
82+
```dart
83+
final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
84+
```
85+
86+
你可以使用 `CameraPickerConfig` 来调整选择时的行为。
87+
88+
```dart
89+
final AssetEntity? entity = await CameraPicker.pickFromCamera(
90+
context,
91+
pickerConfig: const CameraPickerConfig(),
92+
);
93+
```
94+
95+
`CameraPickerConfig` 的成员说明:
96+
6097
| 参数名 | 类型 | 描述 | 默认值 |
6198
|------------------------------|---------------------------------|----------------------------------------------------|----------------------------------------|
6299
| enableRecording | `bool` | 选择器是否可以录像 | `false` |
@@ -81,12 +118,6 @@ Flutter SDK:`>=2.0.0` 。
81118
| onEntitySaving | `EntitySaveCallback?` | 在查看器中保存图片时的回调 | null |
82119
| onError | `CameraErrorHandler?` | 拍摄照片过程中的自定义错误处理 | null |
83120

84-
### 简单的使用方法
85-
86-
```dart
87-
final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
88-
```
89-
90121
## 常见问题 💭
91122

92123
### iOS 上的预览在旋转时行为诡异

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Based on `camera` for camera functions and `photo_manager` for asset implementat
2424
- [Preparing for use 🍭](#preparing-for-use-)
2525
- [Usage 📖](#usage-)
2626
- [Simple usage](#simple-usage)
27+
- [With configurations](#with-configurations)
2728
- [Frequently asked question 💭](#frequently-asked-question-)
2829
- [Why the orientation behavior is strange on iOS?](#why-the-orientation-behavior-is-strange-on-ios)
2930
- [Contributors ✨](#contributors-)
@@ -59,6 +60,25 @@ Flutter SDK: `>=2.0.0` .
5960

6061
## Usage 📖
6162

63+
### Simple usage
64+
65+
```dart
66+
final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
67+
```
68+
69+
### With configurations
70+
71+
Use `CameraPickerConfig` for more picking behaviors.
72+
73+
```dart
74+
final AssetEntity? entity = await CameraPicker.pickFromCamera(
75+
context,
76+
pickerConfig: const CameraPickerConfig(),
77+
);
78+
```
79+
80+
Fields in `CameraPickerConfig`:
81+
6282
| Name | Type | Description | Default Value |
6383
|------------------------------|---------------------------------|-------------------------------------------------------------------------------------------------------|----------------------------------------|
6484
| enableRecording | `bool` | Whether the picker can record video. | `false` |
@@ -83,12 +103,6 @@ Flutter SDK: `>=2.0.0` .
83103
| onEntitySaving | `EntitySaveCallback?` | The callback type define for saving entity in the viewer. | null |
84104
| onError | `CameraErrorHandler?` | The error handler when any error occurred during the picking process. | null |
85105

86-
### Simple usage
87-
88-
```dart
89-
final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
90-
```
91-
92106
## Frequently asked question 💭
93107

94108
### Why the orientation behavior is strange on iOS?

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class _MyHomePageState extends State<MyHomePage> {
4545
try {
4646
final AssetEntity? _entity = await CameraPicker.pickFromCamera(
4747
context,
48-
enableRecording: true,
48+
pickerConfig: const CameraPickerConfig(enableRecording: true),
4949
);
5050
if (_entity != null && entity != _entity) {
5151
entity = _entity;

lib/src/constants/config.dart

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
///
2+
/// [Author] Alex (https://github.com/AlexV525)
3+
/// [Date] 2022/2/14 12:05
4+
///
5+
import 'package:camera/camera.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter/services.dart';
8+
9+
import '../delegates/camera_picker_text_delegate.dart';
10+
import '../internals/type_defs.dart';
11+
12+
/// {@template wechat_camera_picker.CameraPickerConfig}
13+
/// Configurations for the [CameraPicker].
14+
/// [CameraPicker] 的配置项
15+
/// {@endtemplate}
16+
class CameraPickerConfig {
17+
const CameraPickerConfig({
18+
this.enableRecording = false,
19+
this.onlyEnableRecording = false,
20+
this.enableTapRecording = false,
21+
this.enableAudio = true,
22+
this.enableSetExposure = true,
23+
this.enableExposureControlOnPoint = true,
24+
this.enablePinchToZoom = true,
25+
this.enablePullToZoomInRecord = true,
26+
this.shouldDeletePreviewFile = false,
27+
this.shouldAutoPreviewVideo = false,
28+
this.maximumRecordingDuration = const Duration(seconds: 15),
29+
this.theme,
30+
this.textDelegate,
31+
this.cameraQuarterTurns = 0,
32+
this.resolutionPreset = ResolutionPreset.max,
33+
this.imageFormatGroup = ImageFormatGroup.unknown,
34+
this.preferredLensDirection = CameraLensDirection.back,
35+
this.lockCaptureOrientation,
36+
this.foregroundBuilder,
37+
this.onEntitySaving,
38+
this.onError,
39+
}) : assert(
40+
enableRecording == true || onlyEnableRecording != true,
41+
'Recording mode error.',
42+
);
43+
44+
/// Whether the picker can record video.
45+
/// 选择器是否可以录像
46+
final bool enableRecording;
47+
48+
/// Whether the picker can record video.
49+
/// 选择器是否可以录像
50+
final bool onlyEnableRecording;
51+
52+
/// Whether allow the record can start with single tap.
53+
/// 选择器是否可以单击录像
54+
///
55+
/// It only works when [onlyEnableRecording] is true.
56+
/// 仅在 [onlyEnableRecording] 为 true 时生效。
57+
final bool enableTapRecording;
58+
59+
/// Whether the picker should record audio.
60+
/// 选择器录像时是否需要录制声音
61+
final bool enableAudio;
62+
63+
/// Whether users can set the exposure point by tapping.
64+
/// 用户是否可以在界面上通过点击设定曝光点
65+
final bool enableSetExposure;
66+
67+
/// Whether users can adjust exposure according to the set point.
68+
/// 用户是否可以根据已经设置的曝光点调节曝光度
69+
final bool enableExposureControlOnPoint;
70+
71+
/// Whether users can zoom the camera by pinch.
72+
/// 用户是否可以在界面上双指缩放相机对焦
73+
final bool enablePinchToZoom;
74+
75+
/// Whether users can zoom by pulling up when recording video.
76+
/// 用户是否可以在录制视频时上拉缩放
77+
final bool enablePullToZoomInRecord;
78+
79+
/// {@template wechat_camera_picker.shouldDeletePreviewFile}
80+
/// Whether the preview file will be delete when pop.
81+
/// 返回页面时是否删除预览文件
82+
/// {@endtemplate}
83+
final bool shouldDeletePreviewFile;
84+
85+
/// {@template wechat_camera_picker.shouldAutoPreviewVideo}
86+
/// Whether the video should be played instantly in the preview.
87+
/// 在预览时是否直接播放视频
88+
/// {@endtemplate}
89+
final bool shouldAutoPreviewVideo;
90+
91+
/// The maximum duration of the video recording process.
92+
/// 录制视频最长时长
93+
///
94+
/// Defaults to 15 seconds, allow `null` for unrestricted video recording.
95+
/// 默认为 15 秒,可以使用 `null` 来设置无限制的视频录制
96+
final Duration? maximumRecordingDuration;
97+
98+
/// Theme data for the picker.
99+
/// 选择器的主题
100+
final ThemeData? theme;
101+
102+
/// The number of clockwise quarter turns the camera view should be rotated.
103+
/// 摄像机视图顺时针旋转次数,每次90度
104+
final int cameraQuarterTurns;
105+
106+
/// Text delegate that controls text in widgets.
107+
/// 控制部件中的文字实现
108+
final CameraPickerTextDelegate? textDelegate;
109+
110+
/// Present resolution for the camera.
111+
/// 相机的分辨率预设
112+
final ResolutionPreset resolutionPreset;
113+
114+
/// The [ImageFormatGroup] describes the output of the raw image format.
115+
/// 输出图像的格式描述
116+
final ImageFormatGroup imageFormatGroup;
117+
118+
/// Which lens direction is preferred when first using the camera,
119+
/// typically with the front or the back direction.
120+
/// 首次使用相机时首选的镜头方向,通常是前置或后置。
121+
final CameraLensDirection preferredLensDirection;
122+
123+
/// The foreground widget builder which will cover the whole camera preview.
124+
/// 覆盖在相机预览上方的前景构建
125+
final Widget Function(CameraValue)? foregroundBuilder;
126+
127+
/// Whether the camera should be locked to the specific orientation
128+
/// during captures.
129+
/// 摄像机在拍摄时锁定的旋转角度
130+
final DeviceOrientation? lockCaptureOrientation;
131+
132+
/// {@macro wechat_camera_picker.EntitySaveCallback}
133+
final EntitySaveCallback? onEntitySaving;
134+
135+
/// {@macro wechat_camera_picker.CameraErrorHandler}
136+
final CameraErrorHandler? onError;
137+
}

lib/src/constants/constants.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import '../delegates/camera_picker_text_delegate.dart';
66

77
export 'package:photo_manager/photo_manager.dart';
88

9-
export '../delegates/camera_picker_text_delegate.dart';
10-
export 'screens.dart';
11-
129
class Constants {
1310
const Constants._();
1411

lib/src/constants/screens.dart

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)