我在我的应用程序中使用了 image_picker 包,它对于一张图像工作正常,但现在我想将它用于多张图像并将它们作为 File 存储在数组中。任何想法都可以很棒。
小智 7
添加image_picker的依赖:
image_picker: ^0.8.4+3
Run Code Online (Sandbox Code Playgroud)
然后为 selectImages() 创建一个方法:
final ImagePicker imagePicker = ImagePicker();
List<XFile>? imageFileList = [];
void selectImages() async {
final List<XFile>? selectedImages = await
imagePicker.pickMultiImage();
if (selectedImages!.isNotEmpty) {
imageFileList!.addAll(selectedImages);
}
print("Image List Length:" + imageFileList!.length.toString());
setState((){});
}
Run Code Online (Sandbox Code Playgroud)
创建一个用于显示所选图像的构建器:
return Scaffold(
appBar: AppBar(
title: Text('Multiple Images'),
),
body: SafeArea(
child: Column(
children: [
ElevatedButton(
onPressed: () {
selectImages();
},
child: Text('Select Images'),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.builder(
itemCount: imageFileList!.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return Image.file(File(imageFileList![index].path),
fit: BoxFit.cover,);
}),
),
),
],
),
));
Run Code Online (Sandbox Code Playgroud)
完整的源代码可在 github 链接中找到...
| 归档时间: |
|
| 查看次数: |
24180 次 |
| 最近记录: |