Skip to content

Commit b3bcc97

Browse files
authored
add:td_upload增加自定义上传监听 (Tencent#775)
1 parent ec56271 commit b3bcc97

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

tdesign-component/example/lib/page/td_upload_page.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class TDUploadState extends State<TDUploadPage> {
5454
remotePath: 'https://tdesign.gtimg.com/demo/images/example4.png'),
5555
];
5656
final List<TDUploadFile> files6 = [];
57+
final List<TDUploadFile> files7 = [];
58+
59+
void onUploadTap() {
60+
print('点击上传');
61+
setState(() {
62+
files7.add(TDUploadFile(
63+
key: files7.length + 1,
64+
remotePath: 'https://tdesign.gtimg.com/demo/images/example1.png'));
65+
});
66+
}
5767

5868
void onValueChanged(List<TDUploadFile> fileList, List<TDUploadFile> value,
5969
TDUploadType event) {
@@ -103,6 +113,7 @@ class TDUploadState extends State<TDUploadPage> {
103113
ExampleItem(desc: '单选上传', builder: _uploadSingle),
104114
ExampleItem(desc: '单选上传(替换)', builder: _uploadSingleWithReplace),
105115
ExampleItem(desc: '多选上传', builder: _uploadMultiple),
116+
ExampleItem(desc: '自定义upload按钮事件', builder: _uploadTap),
106117
],
107118
),
108119
ExampleModule(
@@ -190,6 +201,22 @@ class TDUploadState extends State<TDUploadPage> {
190201
));
191202
}
192203

204+
@Demo(group: 'upload')
205+
Widget _uploadTap(BuildContext context) {
206+
return wrapDemoContainer('自定义upload按钮事件',
207+
child: TDUpload(
208+
files: files7,
209+
multiple: true,
210+
max: 9,
211+
onUploadTap: onUploadTap,
212+
onClick: onClick,
213+
onCancel: onCancel,
214+
onError: print,
215+
onValidate: print,
216+
onChange: ((files, type) => onValueChanged(files7, files, type)),
217+
));
218+
}
219+
193220
@Demo(group: 'upload')
194221
Widget _uploadLoading(BuildContext context) {
195222
return wrapDemoContainer('上传图片',

tdesign-component/lib/src/components/upload/td_upload.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class TDUpload extends StatefulWidget {
8787
this.wrapSpacing,
8888
this.wrapRunSpacing,
8989
this.wrapAlignment,
90+
this.onUploadTap
9091
}) : super(key: key);
9192

9293
/// 控制展示的文件列表
@@ -146,6 +147,9 @@ class TDUpload extends StatefulWidget {
146147
/// 多图对齐方式
147148
final WrapAlignment? wrapAlignment;
148149

150+
///自定义upload按钮事件
151+
final VoidCallback? onUploadTap;
152+
149153
@override
150154
State<TDUpload> createState() => _TDUploadState();
151155
}
@@ -331,8 +335,12 @@ class _TDUploadState extends State<TDUpload> {
331335
if (widget.disabled!) {
332336
return;
333337
}
334-
final files = await getMediaFromPicker(widget.multiple);
335-
extractImageList(files);
338+
if (widget.onUploadTap != null) {
339+
widget.onUploadTap!();
340+
} else {
341+
final files = await getMediaFromPicker(widget.multiple);
342+
extractImageList(files);
343+
}
336344
}),
337345
);
338346
}

0 commit comments

Comments
 (0)