0

我正在使用以下代码在颤振应用程序中选择图像,该应用程序已成功运行。如果我不想上传图片并想删除挑选的图片,我应该怎么做?

Future getImage() async {
    print("get image");

    PickedFile image = await _picker.getImage(source: ImageSource.gallery);

    if (image != null) {
      setState(() {
        final File file = File(image.path);
        avatarImageFile = file;
        isLoading1 = true;

      });
    }
  }

显示图像的代码

Stack(

                      children: <Widget>[
                        (avatarImageFile == null)
                            ? (photoUrl != ''
                            ? Material(
                          child: Image(image: AssetImage('assets/user.jpg'),
                            width: 100,
                            height: 100,
                            fit: BoxFit.cover,


                          ),
                          clipBehavior:Clip.hardEdge ,
                          borderRadius: BorderRadius.all(Radius.circular(25.0)),
                        )
                            : Icon(
                          Icons.account_circle,
                          size: 90.0,
                          color: Colors.grey,
                        ))
                            : Material(
                          child: Image.file(
                            avatarImageFile,
                            width: 90.0,
                            height: 90.0,
                            fit: BoxFit.cover,
                          ),
                          borderRadius: BorderRadius.all(Radius.circular(25.0)),
                          clipBehavior: Clip.hardEdge,
                        ),
                        IconButton(
                          icon: Icon(
                            Icons.camera,
                            color: Colors.orange.withOpacity(0.5),
                          ),
                          onPressed: getImage,
                          padding: EdgeInsets.all(30.0),
                          splashColor: Colors.transparent,
                          highlightColor: Colors.grey,
                          iconSize: 30.0,
                        ),
                      ],
                    ),

现在我只是选择图像并尝试删除选择的图像,我的意思是它应该为空,上传图像是在上下文的后期阶段

4

2 回答 2

2

您可以在 IconButton 中使用 setState 再次将其设置为 null,如下所示:

IconButton(
  icon: Icon(Icons.clear),
  onPressed: () {
    setState(() {
      avatarImageFile = null;
    });
  },
),
于 2020-07-15T11:47:45.093 回答
0

以这种方式用墨水瓶包裹它,当您点击图像时,图像选择器会重新打开并可以覆盖其他图像

 Inkwell(
   onTap: (){
   getImage()}
   Image.file(
     avatarImageFile,
     width: 90.0,
     height: 90.0,
    fit: BoxFit.cover,
   ),
于 2020-07-15T11:53:40.947 回答