Flutter 仿IOS 底部弹出框选择拍照和相册
涉及到的库:
image_picker:选择图片
flutter_luban:压缩图片
path_provider:获取原生的sd路径
void showImgDialog(int type){
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (BuildContext context) {
return Container(
height: 171,
margin: EdgeInsets.only(left: 15, right: 15), //控制底部的距离
child: Column(
children: [
Container(
height: 101,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: [
InkWell(
onTap: () async {
Navigator.pop(context);
var imgPath = await _getImageFromCamera();
switch(type){
case 0:
notifyImg = imgPath;
break;
case 1:
emergencyImg = imgPath;
break;
case 2:
promiseImg = imgPath;
break;
}
setState(() {});
},
child: Container(
height: 50,
child: Center(
child: Text(
'拍照',
style: TextStyle(
// fontSize: Config.fontSize17,
letterSpacing: 2.0,
fontWeight: FontWeight.w200),
),
),
),
),
Divider(
height: 1,
color: Colors.grey,
),
InkWell(
onTap: () async {
Navigator.pop(context);
var imgPath = await _getImageFromGallery();
switch(type){
case 0:
notifyImg = imgPath;
break;
case 1:
emergencyImg = imgPath;
break;
case 2:
promiseImg = imgPath;
break;
}
setState(() {});
},
child: Container(
height: 50,
child: Center(
child: Text(
'本地相册',
style: TextStyle(
// fontSize: Config.fontSize17,
letterSpacing: 2.0,
fontWeight: FontWeight.w200),
),
),
),
),
],
),
),
InkWell(
onTap: (){
NavigatorUtils.goBack(context);
},
child: Container(
margin: EdgeInsets.only(top: 10, bottom: 10,),
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: Text(
'取消',
style: TextStyle(
color: Colors.red,
// fontSize: Config.fontSize17,
fontWeight: FontWeight.w200),
),
),
),
),
],
),
);
});
}
Future _getImageFromCamera() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera, maxWidth: 400);
return await compassImage(image);
}
//相册选择
Future _getImageFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
return await compassImage(image);
}
Future compassImage(File imageFile) async{
//压缩对象
CompressObject compressObject = CompressObject(
imageFile:imageFile, //image
path: await FileUtils.getTemDirectory(), //compress to path
quality: 85,//first compress quality, default 80
step: 9,//compress quality step, The bigger the fast, Smaller is more accurate, default 6
mode: CompressMode.LARGE2SMALL,//default AUTO
);
return await Luban.compressImage(compressObject);
}