forked from Tencent/tdesign-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd_upload_page.dart
More file actions
278 lines (260 loc) · 8.01 KB
/
td_upload_page.dart
File metadata and controls
278 lines (260 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../annotation/demo.dart';
import '../base/example_widget.dart';
class TDUploadPage extends StatefulWidget {
const TDUploadPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => TDUploadState();
}
class TDUploadState extends State<TDUploadPage> {
final List<TDUploadFile> files1 = [];
final List<TDUploadFile> files2 = [
TDUploadFile(
key: 1,
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'),
TDUploadFile(
key: 2,
remotePath: 'https://tdesign.gtimg.com/demo/images/example2.png'),
TDUploadFile(
key: 3,
remotePath: 'https://tdesign.gtimg.com/demo/images/example3.png'),
TDUploadFile(
key: 4,
remotePath: 'https://tdesign.gtimg.com/demo/images/example4.png'),
];
final List<TDUploadFile> files3 = [
TDUploadFile(
key: 1,
status: TDUploadFileStatus.loading,
loadingText: '上传中...',
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'),
TDUploadFile(
key: 2,
status: TDUploadFileStatus.loading,
progress: 68,
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'),
];
final List<TDUploadFile> files4 = [
TDUploadFile(
key: 1,
status: TDUploadFileStatus.retry,
retryText: '重新上传',
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'),
];
final List<TDUploadFile> files5 = [
TDUploadFile(
key: 1,
status: TDUploadFileStatus.error,
errorText: '上传失败',
remotePath: 'https://tdesign.gtimg.com/demo/images/example4.png'),
];
final List<TDUploadFile> files6 = [];
final List<TDUploadFile> files7 = [];
void onUploadTap() {
print('点击上传');
setState(() {
files7.add(TDUploadFile(
key: files7.length + 1,
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'));
});
}
void onValueChanged(List<TDUploadFile> fileList, List<TDUploadFile> value,
TDUploadType event) {
switch (event) {
case TDUploadType.add:
setState(() {
fileList.addAll(value);
});
break;
case TDUploadType.remove:
setState(() {
fileList.removeWhere((element) => element.key == value[0].key);
});
break;
case TDUploadType.replace:
setState(() {
final firstReplaceFile = value.first;
final index =
fileList.indexWhere((file) => file.key == firstReplaceFile.key);
if (index != -1) {
fileList[index] = firstReplaceFile;
}
});
break;
}
}
void onClick(int key) {
print('点击 $key');
}
void onCancel() {
print('取消');
}
@override
Widget build(BuildContext context) {
return ExamplePage(
title: tdTitle(),
exampleCodeGroup: 'upload',
desc:
'用于相册读取或拉起拍照的图片上传功能。${PlatformUtil.isWeb ? "Web端不支持读取本地图片,请前往移动端体验。" : ""}',
children: [
ExampleModule(
title: '组件类型',
children: [
ExampleItem(desc: '单选上传', builder: _uploadSingle),
ExampleItem(desc: '单选上传(替换)', builder: _uploadSingleWithReplace),
ExampleItem(desc: '多选上传', builder: _uploadMultiple),
ExampleItem(desc: '自定义upload按钮事件', builder: _uploadTap),
],
),
ExampleModule(
title: '组件状态',
children: [
ExampleItem(desc: '加载状态', builder: _uploadLoading),
ExampleItem(desc: '重新上传', builder: _uploadRetry),
ExampleItem(desc: '上传失败', builder: _uploadError),
],
),
],
test: [
ExampleItem(
ignoreCode: true,
desc: '单选快速替换, 大小和图形测试',
builder: _uploadSingleWithReplace),
ExampleItem(
ignoreCode: true, desc: '上传文件大小限制,10KB', builder: _uploadSizeLimit),
],
);
}
Widget wrapDemoContainer(String title, {required Widget child}) {
return Container(
padding: const EdgeInsets.all(16),
color: TDTheme.of(context).bgColorContainer,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TDText(
title,
style: const TextStyle(fontSize: 16),
),
const SizedBox(
height: 16,
),
child
],
),
);
}
@Demo(group: 'upload')
Widget _uploadSingle(BuildContext context) {
return wrapDemoContainer('单选上传',
child: TDUpload(
files: files1,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files1, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadSingleWithReplace(BuildContext context) {
return wrapDemoContainer('单选上传(替换)',
child: TDUpload(
files: files6,
width: 60,
height: 60,
type: TDUploadBoxType.circle,
enabledReplaceType: true,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files6, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadMultiple(BuildContext context) {
return wrapDemoContainer('多选上传',
child: TDUpload(
files: files2,
multiple: true,
max: 9,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files2, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadTap(BuildContext context) {
return wrapDemoContainer('自定义upload按钮事件',
child: TDUpload(
files: files7,
multiple: true,
max: 9,
onUploadTap: onUploadTap,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files7, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadLoading(BuildContext context) {
return wrapDemoContainer('上传图片',
child: TDUpload(
files: files3,
multiple: true,
max: 9,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files3, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadRetry(BuildContext context) {
return wrapDemoContainer('上传图片',
child: TDUpload(
files: files4,
multiple: true,
max: 9,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files4, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadError(BuildContext context) {
return wrapDemoContainer('上传图片',
child: TDUpload(
files: files5,
multiple: true,
max: 9,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
onChange: ((files, type) => onValueChanged(files5, files, type)),
));
}
@Demo(group: 'upload')
Widget _uploadSizeLimit(BuildContext context) {
return wrapDemoContainer('限制10KB',
child: TDUpload(
files: files1,
onClick: onClick,
onCancel: onCancel,
onError: print,
onValidate: print,
sizeLimit: 10,
onChange: ((files, type) => onValueChanged(files1, files, type)),
));
}
}