关于flutter中的图像选择器插件,我需要一些帮助。我想让用户从他/她想要的任何地方选择图像,例如相机,画廊,Google驱动器/照片或其他任何地方,而不仅仅是一个选项。
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class UserImagePicker extends StatefulWidget {
@override
_UserImagePickerState createState() => _UserImagePickerState();
}
class _UserImagePickerState extends State<UserImagePicker> {
File _pickedImage;
void _pickImage() async {
final pickedImageFile =
await ImagePicker().getImage(source: ImageSource.gallery);
setState(() {
_pickedImage = File(pickedImageFile.path);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
CircleAvatar(
radius: 40,
backgroundColor: Colors.grey,
backgroundImage:
_pickedImage != null ? FileImage(_pickedImage) : null,
),
FlatButton.icon(
textColor: Theme.of(context).primaryColor,
onPressed: _pickImage,
icon: Icon(Icons.image),
label: Text('Add Image'),
),
],
);
}
}
答案 0 :(得分:1)
这里是对您的_pickedImage
的修改,以显示警报对话框,以便用户可以选择其图像来源。
void _pickedImage() {
showDialog<ImageSource>(
context: context,
builder: (context) => AlertDialog(
content: Text("Choose image source"),
actions: [
FlatButton(
child: Text("Camera"),
onPressed: () => Navigator.pop(context, ImageSource.camera),
),
FlatButton(
child: Text("Gallery"),
onPressed: () => Navigator.pop(context, ImageSource.gallery),
),
]
),
).then((ImageSource source) async {
if (source != null) {
final pickedFile = await ImagePicker().getImage(source: source);
setState(() => _pickedImage = File(pickedFile.path));
}
});
}