【发布时间】:2020-06-19 06:47:44
【问题描述】:
在我的 Flutter 公关项目中,我使用 Image Picker 插件从 android 移动图库中选择图像,或者用相机捕获图像并在每张图像下方使用删除图标依次显示它们。在点击RaisedButton 从图库中选择图像时,会调用方法imageSelectorGallery()。在setState() 方法内部,我向List 添加了一个SizedBox 和一个delete 图标,即images_captured。我希望images_captured 在SingleChildScrollView 中的Column 内呈现。
但是从图库中选择图像后,什么也没有发生。我还想点击delete 图标并删除其上方的图像。但据我所知,flutter 没有数据绑定机制可以将图像与删除按钮关联起来。
代码如下:
class PrescriptionScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new UserOptionsState();
}
}
class UserOptionsState extends State<PrescriptionScreen> {
//save the result of gallery fileUserOptions
File galleryFile;
//save the result of camera file
File cameraFile;
@override
Widget build(BuildContext context) {
var images_captured=List<Widget>();
//display image selected from gallery
imageSelectorGallery() async {
galleryFile = await ImagePicker.pickImage(
source: ImageSource.gallery,
// maxHeight: 50.0,
// maxWidth: 50.0,
);
print("You selected gallery image : " + galleryFile.path);
setState(() {
var sized_box_indiv= new SizedBox(
height: 200.0,
width: 300.0,
//child: new Card(child: new Text(''+galleryFile.toString())),
//child: new Image.file(galleryFile),
child: galleryFile == null
? new Text('Sorry nothing selected from gallery!!')
: new Image.file(galleryFile),
);
images_captured.add(sized_box_indiv);
var delete_button = IconButton(icon: Icon(Icons.delete), onPressed: () {});
images_captured.add(delete_button);
});
}
//display image selected from camera
imageSelectorCamera() async {
cameraFile = await ImagePicker.pickImage(
source: ImageSource.camera,
//maxHeight: 50.0,
//maxWidth: 50.0,
);
print("You selected camera image : " + cameraFile.path);
setState(() {});
}
return new SingleChildScrollView(
child:Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new RaisedButton(
child: new Text('Select Image from Gallery'),
onPressed: imageSelectorGallery,
),
new RaisedButton(
child: new Text('Select Image from Camera'),
onPressed: imageSelectorCamera,
),
Column(
children: images_captured
),
],
),
);
/* },
),
);*/
}
}
Q1:如何在每张图片下方分别显示delete图标按钮从图库中选择的图片?
Q2:点击delete图标按钮如何删除对应的图片?
我想如果我可以为画廊做到这一点,我也可以为相机拍摄做到这一点......
编辑:
我使用jJuice的答案,选择后的图像显示溢出错误。截图如下:
我的代码是:
class UserOptionsState extends State<PrescriptionScreen> {
//save the result of gallery fileUserOptions
File galleryFile;
//save the result of camera file
File cameraFile;
var images_captured=List<Widget>();
List<File> images = List<File>();
@override
Widget build(BuildContext context) {
//display image selected from gallery
imageSelectorGallery() async {
galleryFile = await ImagePicker.pickImage(
source: ImageSource.gallery,
// maxHeight: 50.0,
// maxWidth: 50.0,
);
images.add(galleryFile);
print("You selected gallery image : " + galleryFile.path);
setState(() {
});
}
//display image selected from camera
imageSelectorCamera() async {
cameraFile = await ImagePicker.pickImage(
source: ImageSource.camera,
//maxHeight: 50.0,
//maxWidth: 50.0,
);
print("You selected camera image : " + cameraFile.path);
setState(() {});
}
return new SingleChildScrollView(
child:Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new RaisedButton(
child: new Text('Select Image from Gallery'),
onPressed: imageSelectorGallery,
),
new RaisedButton(
child: new Text('Select Image from Camera'),
onPressed: imageSelectorCamera,
),
new Container(
// new Column(
// children: <Widget>[
height: 1200,
child:GridView.count(
crossAxisSpacing: 6,
mainAxisSpacing: 6,
crossAxisCount: 3,
children: List.generate(images.length, (index) {
return Column(
children: <Widget>[
Container(
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
child: ClipRRect(
child: Image.file(images[index], fit: BoxFit.cover),
borderRadius: BorderRadius.circular(10),
)
),
GestureDetector(
onTap: () {
setState(() {
images.removeAt(index);
});
},
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Align(
alignment: Alignment.bottomCenter,
child: Icon(Icons.clear, color: Colors.black, size: 20),
),
),
),
]
);
}
),
),
// ]
)
/*displaySelectedFile(galleryFile),
displaySelectedFile(cameraFile)*/
],
),
);
}
Widget displaySelectedFile(File file) {
return new SizedBox(
height: 200.0,
width: 300.0,
//child: new Card(child: new Text(''+galleryFile.toString())),
//child: new Image.file(galleryFile),
child: file == null
? new Text('Sorry nothing selected!!')
: new Image.file(file),
);
}
}
【问题讨论】:
-
如果你想加载多张图片可以使用这个插件pub.dev/packages/multi_image_picker。看看它是否有效,或者您需要更多帮助。
-
我的方法有什么问题?
-
使用您的方法,您只能拥有一台相机和一张画廊图片,因为您为每个使用一个实例。您可以使用页面视图显示它们,对于单个页面,您可以使用带有删除图标的堆栈。
-
新图像被添加到“images_captured”列表中。那他们为什么不出现呢?连一张图片都没有出现?您可以用您的方法发布答案,即:堆栈等吗?删除操作将如何发生?
-
您的图像列表是空的,因为它在构建函数中,并且每次获得图像时,您都会调用 setState 函数,该函数再次调用构建函数并且您的图像列表会再次创建。尝试将
var images_captured=List<Widget>()放在构建函数之外,您将获得所有图像。
标签: flutter flutter-image imagepicker