在我的 Flutter 应用程序中,我显示了一些图像,目前,当我单击它们时,我可以下载它们,但我想更改它,以便当我单击它打开的图像时,有人知道我该怎么做吗?
一个例子是 gmail,您可以在其中接收文件,当您单击它们时,它们会打开,然后您可以选择下载
图像缩略图小部件
ListTile(
leading: Image.network(imageUrl),
title: const Text('Image file'),
subtitle: const Text('Tap for preview'),
onTap: () => showPreview(imageUrl),
)
图像预览对话框
void showPreview(String url) {
showDialog(
context: context,
builder: (c) => AlertDialog(
title: Row(
children: [
const Expanded(child: Text('Image File')),
IconButton(
onPressed: () => download(url),
icon: const Icon(Icons.download))
],
),
content: Image.network(url),
));
}
下载代码
void download(String url) {
http.get(Uri.parse(url)).then((value) {
if (value.statusCode == 200) {
Uint8List bytes = value.bodyBytes;
print('Bytes :: ${bytes.length}');
}
});
}
我使用http包进行下载
欢迎来到 StackOverflow。
要显示来自已知 URL 的图像,您只需使用
Image.network()
这是一个简单的例子:
Image.network(
'https://images.unsplash.com/photo-1501927023255-9063be98970c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1950&q=80',
fit: BoxFit.contain,
),