設置一個按鈕調用 打開showCupertinoModalPopup
FloatingActionButton(onPressed: (){ _showDialog(context); }, child: Text('aaaaaa'),),
打開底部彈出框
void _showDialog(BuildContext cxt){
showCupertinoModalPopup<int>(context: cxt, builder:(cxt){
var dialog =CupertinoActionSheet(
title: Text("請選擇"),
// message: Text('上傳作業!'),
cancelButton: CupertinoActionSheetAction(onPressed: (){
Navigator.pop(cxt,0);
}, child: Text("取消")),
actions: <Widget>[
CupertinoActionSheetAction(onPressed: (){
showImagePicker(1);
Navigator.pop(cxt,1);
}, child: Text('相機')),
CupertinoActionSheetAction(onPressed: (){
showImagePicker(2);
Navigator.pop(cxt,2);
}, child: Text('相冊')),
],
);
return dialog;
});
}
根據選擇的設備進行調用相機或者相冊
showImagePicker(type) async{
/// Specifies the source where the picked image should come from.
enum ImageSource { 這個是調用源碼
/// Opens up the device camera, letting the user to take a new picture.
camera,
/// Opens the user's photo gallery.
gallery,
}
var image = await ImagePicker.pickImage(source: type ==1?ImageSource.camera: ImageSource.gallery); 枚舉類型 設置調用的類型camera 相機 另外一個是本地相冊
setState(() {
_image = image;
});
}
需要引入的包:
import 'package:image_picker/image_picker.dart';
import 'package:flutter/cupertino.dart';