使用Getx Flutter使用相机后如何显示图像?它显示AssetImage('assets / images / kad.jpg'),但拍照后无法显示相机的照片..请帮助我
imageController.dart
class ImageController extends GetxController {
static ImageController get to => Get.find<ImageController>();
File image;
String imagePath;
final _picker = ImagePicker();
Future<void> getImage() async {
final pickedFile = await _picker.getImage(source: ImageSource.camera);
if (pickedFile != null) {
image = File(pickedFile.path);
File imagePath = File(pickedFile.path);
print(imagePath);
} else {
print('No image selected.');
}
}
}
在UI.dart上
class UploadPicture extends StatelessWidget {
final imageController = ImageController.to;
..........
image: DecorationImage(
image: imageController.image == null
? AssetImage('assets/images/kad.jpg')
: Image.file(File(imageController.image.path))),```
it show AssetImage('assets/images/kad.jpg') but after take picture it not show the picture from camera.. please help me
答案 0 :(得分:0)
我认为您错过了getImage()函数中的update()方法
Future<void> getImage() async {
final pickedFile = await _picker.getImage(source: ImageSource.camera);
if (pickedFile != null) {
image = File(pickedFile.path);
File imagePath = File(pickedFile.path);
print(imagePath);
update();
} else {
print('No image selected.');
}
}
答案 1 :(得分:0)
问题解决了。不能将Image.file放在decorationImage下。全部Qt
DecorationImage(image: Image.file(File(imageController.image.path)) //wrong
DecorationImage(image: AssetImage('assets/images/kad.jpg')) //right