Skip to content

Commit 7f60175

Browse files
committed
♻️ Refactored example to allow custom implementations
1 parent 56b2085 commit 7f60175

File tree

8 files changed

+300
-149
lines changed

8 files changed

+300
-149
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
///
2+
/// [Author] Alex (https://github.com/AlexV525)
3+
/// [Date] 2021/5/10 16:56
4+
///
5+
import 'package:flutter/widgets.dart';
6+
7+
class CustomPickMethod {
8+
const CustomPickMethod({
9+
required this.icon,
10+
required this.name,
11+
required this.description,
12+
required this.method,
13+
});
14+
15+
final String icon;
16+
final String name;
17+
final String description;
18+
final void Function(BuildContext context) method;
19+
}

example/lib/constants/picker_model.dart renamed to example/lib/constants/picker_method.dart

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import 'package:flutter/material.dart';
66
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
77
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
88

9-
class PickMethodModel {
10-
const PickMethodModel({
9+
class PickMethod {
10+
const PickMethod({
1111
required this.icon,
1212
required this.name,
1313
required this.description,
1414
required this.method,
1515
});
1616

17-
factory PickMethodModel.image(int maxAssetsCount) {
18-
return PickMethodModel(
17+
factory PickMethod.image(int maxAssetsCount) {
18+
return PickMethod(
1919
icon: '🖼️',
2020
name: 'Image picker',
2121
description: 'Only pick image from device.',
@@ -30,8 +30,8 @@ class PickMethodModel {
3030
);
3131
}
3232

33-
factory PickMethodModel.video(int maxAssetsCount) {
34-
return PickMethodModel(
33+
factory PickMethod.video(int maxAssetsCount) {
34+
return PickMethod(
3535
icon: '🎞',
3636
name: 'Video picker',
3737
description: 'Only pick video from device.',
@@ -46,8 +46,8 @@ class PickMethodModel {
4646
);
4747
}
4848

49-
factory PickMethodModel.audio(int maxAssetsCount) {
50-
return PickMethodModel(
49+
factory PickMethod.audio(int maxAssetsCount) {
50+
return PickMethod(
5151
icon: '🎶',
5252
name: 'Audio picker',
5353
description: 'Only pick audio from device.',
@@ -62,11 +62,11 @@ class PickMethodModel {
6262
);
6363
}
6464

65-
factory PickMethodModel.camera({
65+
factory PickMethod.camera({
6666
required int maxAssetsCount,
6767
required Function(BuildContext, AssetEntity) handleResult,
6868
}) {
69-
return PickMethodModel(
69+
return PickMethod(
7070
icon: '📷',
7171
name: 'Pick from camera',
7272
description: 'Allow pick an asset through camera.',
@@ -99,8 +99,8 @@ class PickMethodModel {
9999
);
100100
}
101101

102-
factory PickMethodModel.common(int maxAssetsCount) {
103-
return PickMethodModel(
102+
factory PickMethod.common(int maxAssetsCount) {
103+
return PickMethod(
104104
icon: '📹',
105105
name: 'Common picker',
106106
description: 'Pick images and videos.',
@@ -115,8 +115,8 @@ class PickMethodModel {
115115
);
116116
}
117117

118-
factory PickMethodModel.threeItemsGrid(int maxAssetsCount) {
119-
return PickMethodModel(
118+
factory PickMethod.threeItemsGrid(int maxAssetsCount) {
119+
return PickMethod(
120120
icon: '🔲',
121121
name: '3 items grid',
122122
description: 'Picker will served as 3 items on cross axis. '
@@ -134,8 +134,8 @@ class PickMethodModel {
134134
);
135135
}
136136

137-
factory PickMethodModel.customFilterOptions(int maxAssetsCount) {
138-
return PickMethodModel(
137+
factory PickMethod.customFilterOptions(int maxAssetsCount) {
138+
return PickMethod(
139139
icon: '⏳',
140140
name: 'Custom filter options',
141141
description: 'Add filter options for the picker.',
@@ -162,8 +162,8 @@ class PickMethodModel {
162162
);
163163
}
164164

165-
factory PickMethodModel.prependItem(int maxAssetsCount) {
166-
return PickMethodModel(
165+
factory PickMethod.prependItem(int maxAssetsCount) {
166+
return PickMethod(
167167
icon: '➕',
168168
name: 'Prepend special item',
169169
description: 'A special item will prepend to the assets grid.',
@@ -185,8 +185,8 @@ class PickMethodModel {
185185
);
186186
}
187187

188-
factory PickMethodModel.noPreview(int maxAssetsCount) {
189-
return PickMethodModel(
188+
factory PickMethod.noPreview(int maxAssetsCount) {
189+
return PickMethod(
190190
icon: '👁️‍🗨️',
191191
name: 'No preview',
192192
description: 'Pick assets like the WhatsApp/MegaTok pattern.',
@@ -209,7 +209,7 @@ class PickMethodModel {
209209
final String name;
210210
final String description;
211211
final Future<List<AssetEntity>?> Function(
212-
BuildContext,
213-
List<AssetEntity>,
212+
BuildContext context,
213+
List<AssetEntity> selectedAssets,
214214
) method;
215215
}

example/lib/customs/CONTRIBUTING.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Contribute custom implementations
2+
3+
This folder allows people to contribute their delegates, use case, or even a brand-new picker.
4+
5+
To add your works, make sure you follow this instruction.
6+
7+
## Ensure that your picker is worth to contribute
8+
9+
People usually have their own apps to build, and the thing like the picker is not always the same between apps.
10+
However, some patterns can be consumed using arguments and fields override, without creating a new picker/delegate.
11+
Please think twice before you confirmed it should be pushed as a new implementation example.
12+
13+
## Contribute steps
14+
15+
1. Fork this project.
16+
2. Create a new branch named `custom-{write-your-implementation-name}`.
17+
3. Create a new picker dart file named `{your_implementation_name}_asset_picker.dart` in the `pickers` folder.
18+
4. Brought all your implementations into the file you've just created.
19+
5. Add a `CustomPickMethod` at the `custom_picker_page.dart` file in `pickMethods`
20+
in order to build an entrance to your picker.
21+
6. Submit your PR with proper explain how your picker works.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
///
2+
/// [Author] Alex (https://github.com/AlexV525)
3+
/// [Date] 2020-11-01 02:05
4+
///
5+
import 'package:flutter/material.dart';
6+
import 'package:wechat_assets_picker_demo/customs/pickers/directory_file_asset_picker.dart';
7+
8+
import '../constants/custom_pick_method.dart';
9+
import '../constants/extensions.dart';
10+
11+
class CustomPickersPage extends StatefulWidget {
12+
@override
13+
_CustomPickerPageState createState() => _CustomPickerPageState();
14+
}
15+
16+
class _CustomPickerPageState extends State<CustomPickersPage>
17+
with AutomaticKeepAliveClientMixin {
18+
ThemeData get currentTheme => context.themeData;
19+
20+
List<CustomPickMethod> get pickMethods {
21+
return <CustomPickMethod>[
22+
CustomPickMethod(
23+
icon: '🗄',
24+
name: 'Directory+File picker',
25+
description: 'The picker is built with `Directory` and `File` '
26+
'as entities, which allows users to display/select `File` '
27+
'through `Directory`.',
28+
method: (BuildContext context) => Navigator.of(context).push<void>(
29+
MaterialPageRoute<void>(
30+
builder: (_) => const DirectoryFileAssetPicker(),
31+
),
32+
),
33+
),
34+
];
35+
}
36+
37+
Widget methodItemBuilder(BuildContext _, int index) {
38+
final CustomPickMethod model = pickMethods[index];
39+
return InkWell(
40+
onTap: () => model.method(context),
41+
child: Container(
42+
padding: const EdgeInsets.symmetric(
43+
horizontal: 30.0,
44+
vertical: 10.0,
45+
),
46+
child: Row(
47+
children: <Widget>[
48+
Container(
49+
margin: const EdgeInsets.all(2.0),
50+
width: 48,
51+
height: 48,
52+
child: Center(
53+
child: Text(
54+
model.icon,
55+
style: const TextStyle(fontSize: 24.0),
56+
),
57+
),
58+
),
59+
const SizedBox(width: 12.0),
60+
Expanded(
61+
child: Column(
62+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
63+
crossAxisAlignment: CrossAxisAlignment.start,
64+
children: <Widget>[
65+
Text(
66+
model.name,
67+
style: const TextStyle(
68+
fontSize: 18.0,
69+
fontWeight: FontWeight.bold,
70+
),
71+
),
72+
const SizedBox(height: 5),
73+
Text(
74+
model.description,
75+
style: context.themeData.textTheme.caption,
76+
overflow: TextOverflow.fade,
77+
),
78+
],
79+
),
80+
),
81+
const Icon(Icons.chevron_right, color: Colors.grey),
82+
],
83+
),
84+
),
85+
);
86+
}
87+
88+
Widget get methodListView {
89+
return Expanded(
90+
child: ListView.builder(
91+
padding: const EdgeInsets.symmetric(vertical: 10.0),
92+
itemCount: pickMethods.length,
93+
itemBuilder: methodItemBuilder,
94+
),
95+
);
96+
}
97+
98+
Widget get tips {
99+
return const Padding(
100+
padding: EdgeInsets.all(20.0),
101+
child: Text(
102+
'This page contains customized pickers with different asset types, '
103+
'different UI layouts, or some use case for specific apps. '
104+
'Contribute to add your custom picker are welcomed.',
105+
),
106+
);
107+
}
108+
109+
@override
110+
bool get wantKeepAlive => true;
111+
112+
@override
113+
Widget build(BuildContext context) {
114+
super.build(context);
115+
return Column(children: <Widget>[tips, methodListView]);
116+
}
117+
}

0 commit comments

Comments
 (0)