【问题标题】:How to Show image after using camera using Getx Flutter?使用 Getx Flutter 使用相机后如何显示图像?
【发布时间】:2021-02-18 04:36:03
【问题描述】:

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

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    我认为你错过了 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.');
        }
      }
    

    【讨论】:

      【解决方案2】:

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

      DecorationImage(image: Image.file(File(imageController.image.path)) //wrong  
      
      DecorationImage(image: AssetImage('assets/images/kad.jpg')) //right
      
      

      【讨论】:

        猜你喜欢
        • 2021-03-06
        • 2022-12-14
        • 1970-01-01
        • 2022-11-08
        • 2023-03-24
        • 2021-01-24
        • 2016-10-07
        • 2021-11-15
        • 2022-07-01
        相关资源
        最近更新 更多