Skip to content

Commit be22c8a

Browse files
committed
🎨 Improve implements in the example
1 parent 067dab8 commit be22c8a

File tree

4 files changed

+438
-569
lines changed

4 files changed

+438
-569
lines changed

example/lib/constants/picker_model.dart

Lines changed: 196 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
///
55
import 'package:flutter/material.dart';
66
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
7-
8-
import '../main.dart';
7+
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
98

109
class PickMethodModel {
1110
const PickMethodModel({
@@ -15,29 +14,202 @@ class PickMethodModel {
1514
required this.method,
1615
});
1716

17+
factory PickMethodModel.image(int maxAssetsCount) {
18+
return PickMethodModel(
19+
icon: '🖼️',
20+
name: 'Image picker',
21+
description: 'Only pick image from device.',
22+
method: (BuildContext context, List<AssetEntity> assets) {
23+
return AssetPicker.pickAssets(
24+
context,
25+
maxAssets: maxAssetsCount,
26+
selectedAssets: assets,
27+
requestType: RequestType.image,
28+
);
29+
},
30+
);
31+
}
32+
33+
factory PickMethodModel.video(int maxAssetsCount) {
34+
return PickMethodModel(
35+
icon: '🎞',
36+
name: 'Video picker',
37+
description: 'Only pick video from device.',
38+
method: (BuildContext context, List<AssetEntity> assets) {
39+
return AssetPicker.pickAssets(
40+
context,
41+
maxAssets: maxAssetsCount,
42+
selectedAssets: assets,
43+
requestType: RequestType.video,
44+
);
45+
},
46+
);
47+
}
48+
49+
factory PickMethodModel.audio(int maxAssetsCount) {
50+
return PickMethodModel(
51+
icon: '🎶',
52+
name: 'Audio picker',
53+
description: 'Only pick audio from device.',
54+
method: (BuildContext context, List<AssetEntity> assets) {
55+
return AssetPicker.pickAssets(
56+
context,
57+
maxAssets: maxAssetsCount,
58+
selectedAssets: assets,
59+
requestType: RequestType.audio,
60+
);
61+
},
62+
);
63+
}
64+
65+
factory PickMethodModel.camera({
66+
required int maxAssetsCount,
67+
required Function(BuildContext, AssetEntity) handleResult,
68+
}) {
69+
return PickMethodModel(
70+
icon: '📷',
71+
name: 'Pick from camera',
72+
description: 'Allow pick an asset through camera.',
73+
method: (BuildContext context, List<AssetEntity> assets) {
74+
return AssetPicker.pickAssets(
75+
context,
76+
maxAssets: maxAssetsCount,
77+
selectedAssets: assets,
78+
requestType: RequestType.common,
79+
specialItemPosition: SpecialItemPosition.prepend,
80+
specialItemBuilder: (BuildContext context) {
81+
return GestureDetector(
82+
behavior: HitTestBehavior.opaque,
83+
onTap: () async {
84+
final AssetEntity? result = await CameraPicker.pickFromCamera(
85+
context,
86+
enableRecording: true,
87+
);
88+
if (result != null) {
89+
handleResult(context, result);
90+
}
91+
},
92+
child: const Center(
93+
child: Icon(Icons.camera_enhance, size: 42.0),
94+
),
95+
);
96+
},
97+
);
98+
},
99+
);
100+
}
101+
102+
factory PickMethodModel.common(int maxAssetsCount) {
103+
return PickMethodModel(
104+
icon: '📹',
105+
name: 'Common picker',
106+
description: 'Pick images and videos.',
107+
method: (BuildContext context, List<AssetEntity> assets) {
108+
return AssetPicker.pickAssets(
109+
context,
110+
maxAssets: maxAssetsCount,
111+
selectedAssets: assets,
112+
requestType: RequestType.common,
113+
);
114+
},
115+
);
116+
}
117+
118+
factory PickMethodModel.threeItemsGrid(int maxAssetsCount) {
119+
return PickMethodModel(
120+
icon: '🔲',
121+
name: '3 items grid',
122+
description: 'Picker will served as 3 items on cross axis. '
123+
'(pageSize must be a multiple of gridCount)',
124+
method: (BuildContext context, List<AssetEntity> assets) {
125+
return AssetPicker.pickAssets(
126+
context,
127+
gridCount: 3,
128+
pageSize: 120,
129+
maxAssets: maxAssetsCount,
130+
selectedAssets: assets,
131+
requestType: RequestType.all,
132+
);
133+
},
134+
);
135+
}
136+
137+
factory PickMethodModel.customFilterOptions(int maxAssetsCount) {
138+
return PickMethodModel(
139+
icon: '⏳',
140+
name: 'Custom filter options',
141+
description: 'Add filter options for the picker.',
142+
method: (
143+
BuildContext context,
144+
List<AssetEntity> assets,
145+
) {
146+
return AssetPicker.pickAssets(
147+
context,
148+
maxAssets: maxAssetsCount,
149+
selectedAssets: assets,
150+
requestType: RequestType.video,
151+
filterOptions: FilterOptionGroup()
152+
..setOption(
153+
AssetType.video,
154+
const FilterOption(
155+
durationConstraint: DurationConstraint(
156+
max: Duration(minutes: 1),
157+
),
158+
),
159+
),
160+
);
161+
},
162+
);
163+
}
164+
165+
factory PickMethodModel.prependItem(int maxAssetsCount) {
166+
return PickMethodModel(
167+
icon: '➕',
168+
name: 'Prepend special item',
169+
description: 'A special item will prepend to the assets grid.',
170+
method: (
171+
BuildContext context,
172+
List<AssetEntity> assets,
173+
) async {
174+
return await AssetPicker.pickAssets(
175+
context,
176+
maxAssets: maxAssetsCount,
177+
selectedAssets: assets,
178+
requestType: RequestType.common,
179+
specialItemPosition: SpecialItemPosition.prepend,
180+
specialItemBuilder: (BuildContext context) {
181+
return const Center(child: Text('Custom Widget'));
182+
},
183+
);
184+
},
185+
);
186+
}
187+
188+
factory PickMethodModel.noPreview(int maxAssetsCount) {
189+
return PickMethodModel(
190+
icon: '👁️‍🗨️',
191+
name: 'No preview',
192+
description: 'Pick assets like the WhatsApp/MegaTok pattern.',
193+
method: (
194+
BuildContext context,
195+
List<AssetEntity> assets,
196+
) async {
197+
return await AssetPicker.pickAssets(
198+
context,
199+
maxAssets: maxAssetsCount,
200+
selectedAssets: assets,
201+
requestType: RequestType.common,
202+
specialPickerType: SpecialPickerType.noPreview,
203+
);
204+
},
205+
);
206+
}
207+
18208
final String icon;
19209
final String name;
20210
final String description;
21-
final Future<List<AssetEntity>?> Function(BuildContext, List<AssetEntity>)
22-
method;
23-
24-
static PickMethodModel common = PickMethodModel(
25-
icon: '🖼️',
26-
name: 'Image picker',
27-
description: 'Simply pick image from device.',
28-
method: (
29-
BuildContext context,
30-
List<AssetEntity> assets,
31-
) async {
32-
return await AssetPicker.pickAssets(
33-
context,
34-
maxAssets: 9,
35-
pathThumbSize: 84,
36-
gridCount: 4,
37-
selectedAssets: assets,
38-
themeColor: themeColor,
39-
requestType: RequestType.image,
40-
);
41-
},
42-
);
211+
final Future<List<AssetEntity>?> Function(
212+
BuildContext,
213+
List<AssetEntity>,
214+
) method;
43215
}

example/lib/pages/custom_picker_page.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class CustomPickerPage extends StatefulWidget {
2525
_CustomPickerPageState createState() => _CustomPickerPageState();
2626
}
2727

28-
class _CustomPickerPageState extends State<CustomPickerPage> {
28+
class _CustomPickerPageState extends State<CustomPickerPage>
29+
with AutomaticKeepAliveClientMixin {
2930
final List<File> fileList = <File>[];
3031

3132
bool isDisplayingDetail = true;
@@ -242,7 +243,12 @@ class _CustomPickerPageState extends State<CustomPickerPage> {
242243
}
243244

244245
@override
246+
bool get wantKeepAlive => true;
247+
248+
@override
249+
@mustCallSuper
245250
Widget build(BuildContext context) {
251+
super.build(context);
246252
return Column(
247253
children: <Widget>[
248254
Expanded(

0 commit comments

Comments
 (0)