osodo
osodo

Reputation: 97

How to use the Simple Dialog widget

I'm trying to use a SimpleDialog to select an image from Gallery or Camera but I'm getting the run error type 'SimpleDialog' is not a subtype of type '(() => void)?' This is my function to call the dialog box

selectImage(context) {
     {
      return SimpleDialog(
        title: Text("Upload Image"),
        children: [
          SimpleDialogOption(
            child: Text("Take Photo"),
            onPressed: handleTakePhoto,
          ),
          SimpleDialogOption(
            child: Text("Upload From Gallery"),
            onPressed: handleChooseFromGallery,
          ),
          SimpleDialogOption(
            onPressed: () => Navigator.pop(context),
            child: Center(
              child: Text("Cancel"),
            ),
          ),
        ],
      );
    }
  }

My handleTakePhoto and handleChooseFromGallery functions are as

handleTakePhoto() async {
    Navigator.pop(context);
    File file = (await ImagePicker().getImage(
      source: ImageSource.camera,
      maxHeight: 675.0,
      maxWidth: 960,
      // imageQuality: 50, //TODO: Look into this
    )) as File;
    setState(() {
      this.file = file;
    });
  }

  handleChooseFromGallery() async {
    Navigator.pop(context);
    File file = (await ImagePicker().getImage(
        source: ImageSource.gallery,
        maxWidth: 960,
        maxHeight: 675,
        preferredCameraDevice: CameraDevice.rear)) as File;
    setState(() {
      this.file = file;
    });
  }

Where have I gone wrong?

Upvotes: 3

Views: 508

Answers (2)

Jitesh Mohite
Jitesh Mohite

Reputation: 34270

SimpleDialog needs to be wrap inside showDialog, to get it displayed

Future<void> selectImage() async {
    await showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text("Upload Image"),
            children: [
              SimpleDialogOption(
                child: Text("Take Photo"),
                onPressed: handleTakePhoto,
              ),
              SimpleDialogOption(
                child: Text("Upload From Gallery"),
                onPressed: handleChooseFromGallery,
              ),
              SimpleDialogOption(
                onPressed: () => Navigator.pop(context),
                child: Center(
                  child: Text("Cancel"),
                ),
              ),
            ],
          );
        });
  }

Output:

enter image description here

Upvotes: 1

Dipak Prajapati
Dipak Prajapati

Reputation: 154

try to this way implement third dialog

 SimpleDialogOption(
        onPressed: () { Navigator.pop(context) },
        child: Center(
          child: Text("Cancel"),
        ),
      ),

Upvotes: 1

Related Questions