在 Flutter 中使用 Imager Picker 依赖
Working with Imager Picker Dependency in Flutter
我的应用程序中有一个功能可以帮助用户从图库中选择图片或使用相机拍照。
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
).whenComplete(() {
setState(() {});
});
if (_imageFile == null) return;
setState(() {
_imageFile = selectedImage;
});
}
我正在使用这个 image_picker dependency and I followed the example I found therein and also examples of possible updates done by others online. From a suggestion on ,建议我在其中添加 whenComplete,我也这样做了。
当我 select 我的图库中的图像时,它不会更新我屏幕上的图像视图小部件。相机选项也不起作用。我可能遗漏了什么?
这是显示图像的图像小部件:
return Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
Image.file(
_imageFile,
fit: BoxFit.cover,
height: 250,
),
Container(
width: 250.0,
height: 100.0,
color: Colors.black54,
child: Column(
children: <Widget>[
Text(
'Change Image',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//camera button
IconButton(
icon: Icon(FontAwesomeIcons.camera),
onPressed: () => _getImage(ImageSource.camera),
color: kThemeStyleButtonFillColour,
),
SizedBox(width: 20.0),
IconButton(
icon: Icon(FontAwesomeIcons.fileImport),
onPressed: () => _getImage(ImageSource.gallery),
color: kThemeStyleButtonFillColour,
),
],
),
],
),
),
SizedBox(height: 32.0),
],
);
在你的情况下,
setState(() {
_imageFile = selectedImage;
});
没有等待选择的图片
试试这个:
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
您在 if
语句中使用了错误的逻辑。
正确的做法,
if(selectedImage != null) { Update image widget here }
我认为 imageFile
变量最初没有分配任何文件,因此条件 imageFile == null
始终为真并且图像小部件永远不会更新。
** 我会更新你的代码,试试这个**
import 'package:image_picker/image_picker.dart';
class check extends StatefulWidget {
@override
_checkState createState() => _checkState();
}
class _checkState extends State<check> {
File _imageFile;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
_imageFile !=null ? Image.file(
_imageFile ,
height: 250,
fit: BoxFit.fill,
) : Text('No image selected.'),
SizedBox(height: 100,),
Container(
width: 250.0,
height: 100.0,
color: Colors.black54,
child: Column(
children: <Widget>[
Text(
'Change Image',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//camera button
IconButton(
icon: Icon(Icons.camera),
onPressed: () => _getImage(ImageSource.camera),
),
SizedBox(width: 20.0),
IconButton(
icon: Icon(Icons.attach_file),
onPressed: () => _getImage(ImageSource.gallery),
),
],
),
],
),
),
SizedBox(height: 32.0),
],
)
);
}
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
}
我能够让这段代码工作,但黑客仍然打败了我,因为它没有意义。
我按照上面给出的建议取消了 whenComplete,因为有等待。
我也将 selectedImage 的声明类型从 File 更改为 var,如下所示:
_getImage(ImageSource source) async {
var selectedImage;
selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
不明白为什么 var 工作而 File 不工作,因为我们知道我们期待一个 File 作为返回值,但它现在工作得很好。
我的应用程序中有一个功能可以帮助用户从图库中选择图片或使用相机拍照。
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
).whenComplete(() {
setState(() {});
});
if (_imageFile == null) return;
setState(() {
_imageFile = selectedImage;
});
}
我正在使用这个 image_picker dependency and I followed the example I found therein and also examples of possible updates done by others online. From a suggestion on
当我 select 我的图库中的图像时,它不会更新我屏幕上的图像视图小部件。相机选项也不起作用。我可能遗漏了什么?
这是显示图像的图像小部件:
return Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
Image.file(
_imageFile,
fit: BoxFit.cover,
height: 250,
),
Container(
width: 250.0,
height: 100.0,
color: Colors.black54,
child: Column(
children: <Widget>[
Text(
'Change Image',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//camera button
IconButton(
icon: Icon(FontAwesomeIcons.camera),
onPressed: () => _getImage(ImageSource.camera),
color: kThemeStyleButtonFillColour,
),
SizedBox(width: 20.0),
IconButton(
icon: Icon(FontAwesomeIcons.fileImport),
onPressed: () => _getImage(ImageSource.gallery),
color: kThemeStyleButtonFillColour,
),
],
),
],
),
),
SizedBox(height: 32.0),
],
);
在你的情况下,
setState(() {
_imageFile = selectedImage;
});
没有等待选择的图片
试试这个:
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
您在 if
语句中使用了错误的逻辑。
正确的做法,
if(selectedImage != null) { Update image widget here }
我认为 imageFile
变量最初没有分配任何文件,因此条件 imageFile == null
始终为真并且图像小部件永远不会更新。
** 我会更新你的代码,试试这个**
import 'package:image_picker/image_picker.dart';
class check extends StatefulWidget {
@override
_checkState createState() => _checkState();
}
class _checkState extends State<check> {
File _imageFile;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
_imageFile !=null ? Image.file(
_imageFile ,
height: 250,
fit: BoxFit.fill,
) : Text('No image selected.'),
SizedBox(height: 100,),
Container(
width: 250.0,
height: 100.0,
color: Colors.black54,
child: Column(
children: <Widget>[
Text(
'Change Image',
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w400,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//camera button
IconButton(
icon: Icon(Icons.camera),
onPressed: () => _getImage(ImageSource.camera),
),
SizedBox(width: 20.0),
IconButton(
icon: Icon(Icons.attach_file),
onPressed: () => _getImage(ImageSource.gallery),
),
],
),
],
),
),
SizedBox(height: 32.0),
],
)
);
}
_getImage(ImageSource source) async {
// ignore: deprecated_member_use
File selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
}
我能够让这段代码工作,但黑客仍然打败了我,因为它没有意义。
我按照上面给出的建议取消了 whenComplete,因为有等待。
我也将 selectedImage 的声明类型从 File 更改为 var,如下所示:
_getImage(ImageSource source) async {
var selectedImage;
selectedImage = await ImagePicker.pickImage(
source: source,
imageQuality: 50,
maxWidth: 400.0,
);
setState(() {
_imageFile = selectedImage;
});
}
不明白为什么 var 工作而 File 不工作,因为我们知道我们期待一个 File 作为返回值,但它现在工作得很好。