Flutter 相机在颤振中的作用
创建一个功能,允许我从多媒体资料中进行选择,并拍摄一张照片,该照片指示我返回显示图像,但图像未显示。当我点击图像时,它允许我选择一个图像并将我转回去;当我使用相机时,它允许我拍照并将我转回去,但图像不显示。我需要从图库中选择的图像和从相机中拍摄的图像显示在屏幕上Flutter 相机在颤振中的作用,flutter,dart,camera,Flutter,Dart,Camera,创建一个功能,允许我从多媒体资料中进行选择,并拍摄一张照片,该照片指示我返回显示图像,但图像未显示。当我点击图像时,它允许我选择一个图像并将我转回去;当我使用相机时,它允许我拍照并将我转回去,但图像不显示。我需要从图库中选择的图像和从相机中拍摄的图像显示在屏幕上 import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; void main() { runApp(new Ma
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(new MaterialApp(
title: "Camera App",
home: LandingScreen(),
));
}
class LandingScreen extends StatefulWidget {
@override
_LandingScreenState createState() => _LandingScreenState();
}
class _LandingScreenState extends State<LandingScreen> {
// GALLERY
File imageFile;
_openGallery(BuildContext context) async{
// ignore: deprecated_member_use
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState(() {
imageFile = picture;
});
Navigator.of(context).pop();
}
// CAMERA
_openCamera(BuildContext context) async{
// ignore: deprecated_member_use
var picture = await ImagePicker.pickImage(source: ImageSource.camera);
this.setState(() {
imageFile = picture;
});
Navigator.of(context).pop();
}
Future<void> _showChoiceDialog(BuildContext context){
return showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text("Make a Choice!"),
content: SingleChildScrollView(child: ListBody(
children: <Widget> [
GestureDetector(
child: Text("Gallery"),
onTap: (){
_openGallery(context);
},
),
Padding(padding: EdgeInsets.all(8.0)),
GestureDetector(
child: Text("Camera"),
onTap: (){
_openCamera(context);
}
)
],
)),
);
});
}
// ignore: missing_return
Widget _decideImageView(){
if (imageFile==null){
return Text("No Image Selected!");
}else{
Image.file(imageFile, width:400,height:400);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Main Screen"),
),
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text("No Image Selected!"),
RaisedButton.icon(
onPressed: () {
_decideImageView();
_showChoiceDialog(context);
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
label: Text(
'Image / Picture',
style: TextStyle(color: Colors.white),
),
icon: Icon(
Icons.camera_alt,
color: Colors.white,
),
textColor: Colors.white,
splashColor: Colors.red,
color: Colors.purple,
),
],
),
),
));
}
}
导入“包装:颤振/材料.省道”;
导入“包:image_picker/image_picker.dart”;
void main(){
runApp(新材料)PP(
标题:“摄像头应用程序”,
主页:着陆屏幕(),
));
}
类LandingScreen扩展StatefulWidget{
@凌驾
_LandingScreenState createState()=>LandingScreenState();
}
类_LandingScreenState扩展状态{
//画廊
文件图像文件;
_openGallery(构建上下文)异步{
//忽略:不推荐的\u成员\u使用
var picture=wait ImagePicker.pickImage(源:ImageSource.gallery);
此.setState(){
图像文件=图片;
});
Navigator.of(context.pop();
}
//摄像机
_openCamera(构建上下文)异步{
//忽略:不推荐的\u成员\u使用
var picture=等待ImagePicker.pickImage(源:ImageSource.camera);
此.setState(){
图像文件=图片;
});
Navigator.of(context.pop();
}
Future\u showChoiceDialog(构建上下文){
返回showDialog(上下文:上下文,生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“做出选择!”),
内容:SingleChildScrollView(子项:ListBody(
儿童:[
手势检测器(
儿童:文本(“图库”),
onTap:(){
_开放画廊(背景);
},
),
填充(填充:EdgeInsets.all(8.0)),
手势检测器(
子:文本(“摄影机”),
onTap:(){
_openCamera(上下文);
}
)
],
)),
);
});
}
//忽略:缺少返回
小部件_decideImageView(){
if(imageFile==null){
返回文本(“未选择图像!”);
}否则{
Image.file(imageFile,宽400,高400);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“主屏幕”),
),
主体:容器(
儿童:中心(
子:列(
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
文本(“未选择图像!”),
RaisedButton.icon(
已按下:(){
_decideImageView();
_showChoiceDialog(上下文);
},
形状:圆形矩形边框(
borderRadius:borderRadius.all(半径.圆形(10.0)),
标签:文本(
“图像/图片”,
样式:TextStyle(颜色:Colors.white),
),
图标:图标(
Icons.camera\u alt,
颜色:颜色,白色,
),
textColor:Colors.white,
颜色:颜色。红色,
颜色:颜色,紫色,
),
],
),
),
));
}
}