Как отредактировать или удалить изображение после загрузки изображения в флаттере - PullRequest
0 голосов
/ 21 октября 2019

У меня есть одна страница с именем BSP_LICENSE_PAGE и страницей BUSINESS DETAILS, поэтому здесь я загружаю изображение на BSP_LICENSE_PAGE и после загрузки изображения на эту страницу оно переходит на страницу BUSINESS_DETAIL, а здесь, на этой странице, я отображаю это изображение. выше есть одна кнопка пера (редактировать), она перенаправит меня на BSP_LICENSE_PAGE, поэтому, когда я пытаюсь редактировать изображения, она не будет редактироваться или не удаляется.

Здесь я прилагаю вывод экранов:

enter image description here

Вот код BSP_LICENSED_PAGE

В этом коде, если условие для редактирования, и еще, если длязагрузка нового изображения

Сохраняю изображение в классе MODEL

  Widget _buildbusinesslicensepicture() {
    if (widget.bspSignupCommonModel.licensed != null) {
      int keyValue = _bspLicenseImages.length;
      return GridView.count(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        crossAxisCount: 5,
        childAspectRatio: 1,
        children: List.generate(
            widget.bspSignupCommonModel.licensed[0].bspLicenseImages.length,
            (index) {
          return Card(
            clipBehavior: Clip.antiAlias,
            child: Stack(
              children: <Widget>[
                Image.file(
                  widget.bspSignupCommonModel.licensed[0]
                      .bspLicenseImages[index].imageFile,
                  width: 500,
                  height: 500,
                ),
                Positioned(
                  right: 5,
                  top: 5,
                  child: InkWell(
                    child: Icon(
                      Icons.remove_circle,
                      size: 20,
                      color: Colors.red,
                    ),
                    onTap: () {
                      setState(() {
                        images.replaceRange(
                            widget.bspSignupCommonModel.licensed[index]
                                .bspLicenseImages.length,
                            widget.bspSignupCommonModel.licensed[index]
                                    .bspLicenseImages.length +
                                1,
                            ['Add Image']);
                      });
                    },
                  ),
                ),
              ],
            ),
          );
        }),
      );
    } else {
      return GridView.count(
        shrinkWrap: true,
        crossAxisCount: 5,
        childAspectRatio: 1,
        children: List.generate(images.length, (index) {
          if (images[index] is ImageUploadModel) {
            ImageUploadModel uploadModel = images[index];

            return Card(
              clipBehavior: Clip.antiAlias,
              child: Stack(
                children: <Widget>[
                  Image.file(
                    uploadModel.imageFile,
                    width: 300,
                    height: 300,
                  ),
                  Positioned(
                    right: 5,
                    top: 5,
                    child: InkWell(
                      child: Icon(
                        Icons.remove_circle,
                        size: 20,
                        color: Colors.red,
                      ),
                      onTap: () {
                        setState(() {
                          images.replaceRange(index, index + 1, ['Add Image']);
                        });
                      },
                    ),
                  ),
                ],
              ),
            );
          } else {
            return Card(
              child: IconButton(
                icon: Icon(Icons.add),
                onPressed: () {
                  _openImagePickerModal(context, index);
                },
              ),
            );
          }
        }),
      );
    }
  }

  void _getImage(BuildContext context, int index, ImageSource source) async {
    Future<File> imagee = ImagePicker.pickImage(source: source);
    imagee.then((file) {
      if (file != null) {
        setState(() {
          _imageFile = imagee;
          getFileImage(index);
        });
      }
    });

    Navigator.pop(context);
  }

  void _openImagePickerModal(BuildContext context, int index) {
    print('Image Picker Modal Called');
    showModalBottomSheet(
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        builder: (BuildContext context) {
          return Container(
            decoration: new BoxDecoration(
                color: Colors.white,
                borderRadius: new BorderRadius.only(
                    topLeft: const Radius.circular(40.0),
                    topRight: const Radius.circular(40.0))),
            height: 150.0,
            padding: EdgeInsets.all(10.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'Pick an image',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                SizedBox(
                  height: 20.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    ClipOval(
                      child: Material(
                        color: colorStyles["primary"], // button color
                        child: InkWell(
                          splashColor: Colors.red, // inkwell color
                          child: SizedBox(
                              width: 56,
                              height: 56,
                              child: Icon(FontAwesomeIcons.image)),
                          onTap: () {
                            _getImage(context, index, ImageSource.gallery);
                          },
                        ),
                      ),
                    ),
                    ClipOval(
                      child: Material(
                        color: colorStyles["primary"], // button color
                        child: InkWell(
                          splashColor: Colors.red, // inkwell color
                          child: SizedBox(
                              width: 56,
                              height: 56,
                              child: Icon(FontAwesomeIcons.camera)),
                          onTap: () {
                            _getImage(context, index, ImageSource.camera);
                          },
                        ),
                      ),
                    ),
                    // FlatButton(
                    //   color: Colors.red,
                    //   textColor: flatButtonColor,
                    //   child: Text('Use Camera'),
                    //   onPressed: () {
                    //     _getImage(context, index, ImageSource.camera);
                    //   },
                    // ),
                    // FlatButton(
                    //   color: Colors.red,
                    //   textColor: flatButtonColor,
                    //   child: Text('Use Gallery'),
                    //   onPressed: () {
                    //     _getImage(context, index, ImageSource.gallery);
                    //   },
                    // ),
                  ],
                ),
              ],
            ),
          );
        });
  }

  void getFileImage(int index) async {
    _imageFile.then((file) async {
      BusinessProfilePicture bspLicenseImage = new BusinessProfilePicture();
      bspLicenseImage.imageFile = file;
      bspLicenseImage.imageUrl = '';
      _bspLicenseImages.add(bspLicenseImage);
      setState(() {
        ImageUploadModel imageUpload = new ImageUploadModel();
        imageUpload.isUploaded = false;
        imageUpload.uploading = false;
        imageUpload.imageFile = file;
        imageUpload.imageUrl = '';
        images.replaceRange(index, index + 1, [imageUpload]);
      });
    });
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...