您需要使用image_picker
用于从相机和图库中获取图像的库,请检查下面的代码,我已将其用于相机和图库。
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:image_picker/image_picker.dart';
class HomeScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}
class _HomeScreen extends State<HomeScreen> {
File imageFile = null;
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text(
"HomeScreen",
textAlign: TextAlign.center,
),
),
body: SafeArea(
top: true,
bottom: true,
child: Align(
alignment: Alignment.center,
child: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.35,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.only(top: 20),
decoration: BoxDecoration(
color: Colors.grey,
shape: BoxShape.circle,
image: DecorationImage(
image: imageFile == null
? AssetImage('images/ic_business.png')
: FileImage(imageFile),
fit: BoxFit.cover))),
SizedBox(
height: 10.0,
),
RaisedButton(
onPressed: () {
_settingModalBottomSheet(context);
},
child: Text("Take Photo")),
],
),
),
));
}
//********************** IMAGE PICKER
Future imageSelector(BuildContext context, String pickerType) async {
switch (pickerType) {
case "gallery":
/// GALLERY IMAGE PICKER
imageFile = await ImagePicker.pickImage(
source: ImageSource.gallery, imageQuality: 90);
break;
case "camera": // CAMERA CAPTURE CODE
imageFile = await ImagePicker.pickImage(
source: ImageSource.camera, imageQuality: 90);
break;
}
if (imageFile != null) {
print("You selected image : " + imageFile.path);
setState(() {
debugPrint("SELECTED IMAGE PICK $imageFile");
});
} else {
print("You have not taken image");
}
}
// Image picker
void _settingModalBottomSheet(context) {
showModalBottomSheet(
context: context,
builder: (BuildContext bc) {
return Container(
child: new Wrap(
children: <Widget>[
new ListTile(
title: new Text('Gallery'),
onTap: () => {
imageSelector(context, "gallery"),
Navigator.pop(context),
}),
new ListTile(
title: new Text('Camera'),
onTap: () => {
imageSelector(context, "camera"),
Navigator.pop(context)
},
),
],
),
);
});
}
}
在清单中,您需要为其输入相机,如下所示
<uses-permission android:name="android.permission.CAMERA" />
还有一张我正在使用默认图像ic_business.png,您可以使用它并确保在pubspec.yaml
图书馆链接是点击这里 https://pub.dev/packages/image_picker
And output will following