【问题标题】:How to remove picked image in flutter?如何在颤动中删除拾取的图像?
【发布时间】:2020-11-04 21:33:09
【问题描述】:

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

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,
                        ),
                      ],
                    ),

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

【问题讨论】:

  • 此代码只选择图片而不是上传它。您需要分享更多代码
  • @UlaşKasım 我已经添加了更多代码,现在我只是删除选择的图像而不是上传它,我的意思是使图像值为空

标签: flutter imagepicker flutter-image


【解决方案1】:

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

 Inkwell(
   onTap: (){
   getImage()}
   Image.file(
     avatarImageFile,
     width: 90.0,
     height: 90.0,
    fit: BoxFit.cover,
   ),

【讨论】:

    【解决方案2】:

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

    IconButton(
      icon: Icon(Icons.clear),
      onPressed: () {
        setState(() {
          avatarImageFile = null;
        });
      },
    ),
    

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2021-04-25
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2016-11-08
      • 2023-02-10
      • 2019-02-09
      相关资源
      最近更新 更多