Home > Blockchain >  How to give the user the choice between Gallery and Camera in ImagePicker
How to give the user the choice between Gallery and Camera in ImagePicker

Time:07-28

I am using ImagePicker in a flutter app, but we should specify the source of the Image. How can I let the choice to the user?

File? selectedImage;

  getImage() async {
    XFile? file = await ImagePicker().pickImage(source: ImageSource.camera);
    selectedImage = File(file!.path);
    setState(() {});
  }

CodePudding user response:

You can show a dialog for to choose option.

getImage() async {
  bool? isCamera = await showDialog(
    context: context,
    builder: (context) => AlertDialog(
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).pop(true);
            },
            child: Text("Camera"),
          ),
          SizedBox(
            height: 20,
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).pop(false);
            },
            child: Text("gallery "),
          ),
        ],
      ),
    ),
  );

  if (isCamera == null) return;

  XFile? file = await ImagePicker()
      .pickImage(source: isCamera ? ImageSource.camera : ImageSource.gallery);
  selectedImage = File(file!.path);
  setState(() {});
}

CodePudding user response:

You need to make two Text one for gallery and one for Camera and on thier onTap() you should pass a function which will be like

void imageGalleryBottomSheet(
      {BuildContext context,
      VoidCallback onCameraTap,
      VoidCallback onGalleryTap}) {
//your code logic

}
  • Related