我有一个名为 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]);
});
});
}