我有一个用Dart编写的小部件,它应该使我能够更改渲染的图片。但是,当我在图像上执行longPress并选择另一张图像时,该图像永远不会更新。我想,这个问题与CACHE有某种联系,但我无法弄清楚。
我使用imageFile变量来存储文件位置。当我覆盖此文件时,我也希望窗口小部件的图片也发生变化,但它不是:(
请帮助!
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'models/profile.dart';
class ImageWidget extends StatefulWidget {
ImageWidget({this.index, this.path, this.mainProfile});
final int index;
final String path;
final Profile mainProfile;
@override
_ImageWidgetState createState() {
return _ImageWidgetState(index: index, path: path, mainProfile: mainProfile,);
}
}
class _ImageWidgetState extends State<ImageWidget> {
_ImageWidgetState({this.index, this.path, this.mainProfile});
final int index;
final String path;
final Profile mainProfile;
File imageFile;
Future<void> pickImage() async {
void closeWindow(File file) async {
File newFile;
if (file != null) {
newFile = await file.copy('$path/image${index}.png');
setState(() {
imageFile = newFile;
});
}
Navigator.pop(context);
}
print("YEP!");
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text("Modify picture"),
children: <Widget>[
SimpleDialogOption(
onPressed: () async {
final File file = await ImagePicker.pickImage(
source: ImageSource.gallery,
);
closeWindow(file);
},
child: const Text('Choose From Gallery'),
),
SimpleDialogOption(
onPressed: () async {
final File file = await ImagePicker.pickImage(
source: ImageSource.camera,
);
closeWindow(file);
},
child: const Text('Take A New Picture'),
),
SimpleDialogOption(
onPressed: () async {
mainProfile.images.removeAt(index);
dispose();
Navigator.pop(context);
},
child: const Text('Delete picture'),
),
]
);
}
);
}
@override
void initState() {
imageFile = File('$path/image$index.png');
super.initState();
}
@override Widget build(BuildContext context) {
return GestureDetector(
onLongPress: () => pickImage(),
child: Container(
width: MediaQuery.of(context).size.width,
child: Image.file(
imageFile,
fit: BoxFit.cover,
),
),
);
}
}