Skip to content

Commit 639e03f

Browse files
committed
⚗️ Experimenting camera picker.
Related to #52 .
1 parent 4c69504 commit 639e03f

File tree

7 files changed

+543
-4
lines changed

7 files changed

+543
-4
lines changed

example/lib/pages/multi_assets_page.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ class _MultiAssetsPageState extends State<MultiAssetsPage> {
7474
);
7575
},
7676
),
77+
PickMethodModel(
78+
icon: '📷',
79+
name: 'Pick from camera',
80+
description: 'Allow pick asset through camera.',
81+
method: (
82+
BuildContext context,
83+
List<AssetEntity> assets,
84+
) async {
85+
return await AssetPicker.pickAssets(
86+
context,
87+
maxAssets: maxAssetsCount,
88+
selectedAssets: assets,
89+
requestType: RequestType.common,
90+
customItemPosition: CustomItemPosition.prepend,
91+
customItemBuilder: (BuildContext context) {
92+
return GestureDetector(
93+
behavior: HitTestBehavior.opaque,
94+
onTap: () async {
95+
final AssetEntity result =
96+
await AssetPicker.pickFromCamera(context);
97+
if (result != null) {
98+
Navigator.of(context)
99+
.pop(<AssetEntity>[...assets, result]);
100+
}
101+
},
102+
child: const Center(
103+
child: Icon(Icons.camera_enhance, size: 42.0),
104+
),
105+
);
106+
},
107+
);
108+
},
109+
),
77110
PickMethodModel(
78111
icon: '📹',
79112
name: 'Common picker',
@@ -147,7 +180,7 @@ class _MultiAssetsPageState extends State<MultiAssetsPage> {
147180
maxAssets: maxAssetsCount,
148181
selectedAssets: assets,
149182
requestType: RequestType.common,
150-
customItemPosition: CustomItemPosition.append,
183+
customItemPosition: CustomItemPosition.prepend,
151184
customItemBuilder: (BuildContext context) {
152185
return const Center(child: Text('Custom Widget'));
153186
},

example/lib/pages/single_assets_page.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ class _SingleAssetPageState extends State<SingleAssetPage> {
7474
);
7575
},
7676
),
77+
PickMethodModel(
78+
icon: '📷',
79+
name: 'Pick from camera',
80+
description: 'Allow pick asset through camera.',
81+
method: (
82+
BuildContext context,
83+
List<AssetEntity> assets,
84+
) async {
85+
return await AssetPicker.pickAssets(
86+
context,
87+
maxAssets: maxAssetsCount,
88+
selectedAssets: assets,
89+
requestType: RequestType.common,
90+
customItemPosition: CustomItemPosition.prepend,
91+
customItemBuilder: (BuildContext context) {
92+
return GestureDetector(
93+
behavior: HitTestBehavior.opaque,
94+
onTap: () async {
95+
final AssetEntity result =
96+
await AssetPicker.pickFromCamera(context);
97+
if (result != null) {
98+
Navigator.of(context).pop(<AssetEntity>[result]);
99+
}
100+
},
101+
child: const Center(
102+
child: Icon(Icons.camera_enhance, size: 42.0),
103+
),
104+
);
105+
},
106+
);
107+
},
108+
),
77109
PickMethodModel(
78110
icon: '📹',
79111
name: 'Common picker',

example/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ dependencies:
1010
flutter:
1111
sdk: flutter
1212

13-
camera: ">=0.5.8+2 <2.0.0"
1413
wechat_assets_picker:
1514
path: ../
1615

lib/src/constants/constants.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/// [Author] Alex (https://github.com/Alex525)
33
/// [Date] 2020/3/31 16:02
44
///
5+
import 'package:flutter/widgets.dart';
6+
57
import 'constants.dart';
68

79
export 'package:flutter_common_exports/flutter_common_exports.dart';
@@ -16,6 +18,8 @@ export 'enums.dart';
1618
class Constants {
1719
const Constants._();
1820

21+
static GlobalKey pickerKey = GlobalKey();
22+
1923
static TextDelegate textDelegate = DefaultTextDelegate();
2024
static SortPathDelegate sortPathDelegate = SortPathDelegate.common;
2125
}

lib/src/widget/asset_picker.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:wechat_assets_picker/wechat_assets_picker.dart';
1818
import '../constants/constants.dart';
1919
import 'builder/fade_image_builder.dart';
2020
import 'builder/slide_page_transition_builder.dart';
21+
import 'camera_picker.dart';
2122
import 'fixed_appbar.dart';
2223

2324
class AssetPicker extends StatelessWidget {
@@ -89,7 +90,6 @@ class AssetPicker extends StatelessWidget {
8990
/// 跳转至选择器的静态方法
9091
static Future<List<AssetEntity>> pickAssets(
9192
BuildContext context, {
92-
Key key,
9393
int maxAssets = 9,
9494
int pageSize = 320,
9595
int pathThumbSize = 200,
@@ -154,7 +154,7 @@ class AssetPicker extends StatelessWidget {
154154
routeDuration: routeDuration,
155155
);
156156
final Widget picker = AssetPicker(
157-
key: key,
157+
key: Constants.pickerKey,
158158
provider: provider,
159159
gridCount: gridCount,
160160
textDelegate: textDelegate,
@@ -184,6 +184,27 @@ class AssetPicker extends StatelessWidget {
184184
}
185185
}
186186

187+
/// Static method to create [AssetEntity] through camera.
188+
/// 通过相机创建 [AssetEntity] 的静态方法
189+
static Future<AssetEntity> pickFromCamera(
190+
BuildContext context, {
191+
bool shouldKeptInLocal = true,
192+
}) async {
193+
final AssetEntity result = await Navigator.of(
194+
context,
195+
rootNavigator: true,
196+
).push<AssetEntity>(
197+
SlidePageTransitionBuilder<AssetEntity>(
198+
builder: CameraPicker(
199+
shouldKeptInLocal: shouldKeptInLocal,
200+
),
201+
transitionCurve: Curves.easeIn,
202+
transitionDuration: const Duration(milliseconds: 300),
203+
),
204+
);
205+
return result;
206+
}
207+
187208
/// Register observe callback with assets changes.
188209
/// 注册资源(图库)变化的监听回调
189210
static void registerObserve([ValueChanged<MethodCall> callback]) {

0 commit comments

Comments
 (0)