小萌是做iOS开发的,虽然是flutter开发,但是xcode还是需要配置的,
在这个info.plist中需要配置
<key>NSPhotoLibraryUsageDescription</key>
<string>Example usage description</string>
<key>NSCameraUsageDescription</key>
<string>Example usage description</string>
配置完成之后就可以了,
下面是完整代码:
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class AuthenticationId extends StatefulWidget {
@override
_AuthenticationIdState createState() => _AuthenticationIdState();
}
class _AuthenticationIdState extends State<AuthenticationId>{
var imgPath;
/*拍照*/
takePhoto() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
imgPath = image;
});
}
/*相册*/
openGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
imgPath = image;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("认证"),
elevation: 0.0,
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: imgPath == null ? Text('请选择拍照'):Image.file(imgPath),
),
RaisedButton(
onPressed: takePhoto,
child: Text("拍照"),
),
RaisedButton(
onPressed: openGallery,
child: Text("选择照片"),
),
],
),
));
}
}