0

使用 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
4

2 回答 2

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.');
    }
  }
于 2020-11-05T13:42:02.560 回答
0

问题解决了。不能将 Image.file 放在 decorationImage 下。全部

DecorationImage(image: Image.file(File(imageController.image.path)) //wrong  

DecorationImage(image: AssetImage('assets/images/kad.jpg')) //right

于 2020-11-06T04:34:25.563 回答