0

我有一个名为 BSP_LICENSE_PAGE 和 BUSINESS DETAILS 页面的页面,所以在这里我在 BSP_LICENSE_PAGE 上传一张图片,在该页面上传一张图片后,它将转到 BUSINESS_DETAIL 页面,在此页面上我正在显示该图片。上面有一个笔(编辑)按钮,它会将我重定向到 BSP_LICENSE_PAGE,因此当我尝试编辑图像时,它不会被编辑或不会被删除。

在这里我附上屏幕的输出:

在此处输入图像描述

这是BSP_LICENSED_PAGE的代码

在此代码中,if 条件用于编辑,否则 if 用于新图像上传

我将图像存储在模型类中

  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]);
      });
    });
  }
4

1 回答 1

1

为了展示你想要达到的目标,我做了一个简单的例子。

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File? _image = null;
  late final _picker = ImagePicker();

  Future getImage() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

    setState(
      () {
        if (image != null) {
          _image = File(image.path);
        } else {
          print('No image selected.');
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Image Picker Example'),
      ),
      body: new Center(
        child: _image == null
            ? new IconButton(
                icon: Icon(Icons.add_box_outlined),
                color: Colors.blue,
                iconSize: 100.0,
                onPressed: () {
                  getImage();
                },
              )
            : Stack(
                children: <Widget>[
                  Container(
                    width: 250.0,
                    height: 320.0,
                    child: new Image.file(_image!),
                  ),
                  Positioned(
                    right: 5.0,
                    child: InkWell(
                      child: Icon(
                        Icons.remove_circle,
                        size: 30,
                        color: Colors.red,
                      ),
                      // This is where the _image value sets to null on tap of the red circle icon
                      onTap: () {
                        setState(
                          () {
                            _image = null;
                          },
                        );
                      },
                    ),
                  )
                ],
              ),
      ),
    );
  }
}

在此处输入图像描述

在示例中,通过点击红色圆圈图标停止显示或删除上传的图像,将文件设置为空:

 onTap: () {
   setState(
     () {
       _image = null;
     },
   );
 },

在你的情况下,它可能是这样的:

 onTap: () {
   setState(
     () {
       imagee = null;
     },
   );
 },
于 2021-07-28T02:30:30.293 回答