按下按钮时,未显示 Flutter 对话框

1cklez4t  于 2023-01-27  发布在  Flutter
关注(0)|答案(2)|浏览(183)
child: RaisedButton(
  color: const Color(0xFF5867DD),
  onPressed: (){
     updateProfilePic();           
  },


 Widget updateProfilePic(){
    return SimpleDialog(
      title: const Text('Select any option'),
             children: <Widget>[
          SimpleDialogOption(
            onPressed: () {  profileImg = ImagePicker.pickImage(source: ImageSource.gallery)
                .whenComplete(() {
              setState(() {});
            }); },
            child: const Text('open gallery'),
          ),
          SimpleDialogOption(
            onPressed: () {   profileImg = ImagePicker.pickImage(source: ImageSource.camera)
                .whenComplete(() {
              setState(() {});
            }); },
            child: const Text('open camera'),
          ),
        ],
    );
  }

我试图实现对话框时,按下按钮。我想从画廊和相机选择图像,使我创建了一个对话框,以选择任何选项上传图片。问题是,当我点击按钮对话框是不可见的。

3pmvbmvn

3pmvbmvn1#

您需要调用showDialog

showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: Text(message),

        );
      });
f87krz0w

f87krz0w2#

Figen Güngör所述,您需要调用showDialog。只是想澄清一下,它是一个Future(函数),而不是一个Widget,因此您的代码如下所示:

Future updateProfilePic(BuildContext context){
    return showDialog(
      context: context,
      builder : (BuildContext context){
        return SimpleDialog(
          title: const Text('Select any option'),
          children: <Widget>[
            SimpleDialogOption(
              onPressed: () {  profileImg = ImagePicker.pickImage(source: ImageSource.gallery)
                  .whenComplete(() {
                setState(() {});
              }); },
              child: const Text('open gallery'),
            ),
            SimpleDialogOption(
              onPressed: () {   profileImg = ImagePicker.pickImage(source: ImageSource.camera)
                  .whenComplete(() {
                setState(() {});
              }); },
              child: const Text('open camera'),
            ),
          ],
        );
      },
    );
  }

相关问题