flutter获取本地图片高度、宽度

  /*获取本地图片宽度
  * */
  getLocalImageWidth(String path){
    int width;
    Completer<int> completer = new Completer<int>();
    Image image = Image.file(File.fromUri(Uri.parse(path)));
    // 预先获取图片信息
    image.image.resolve(new ImageConfiguration()).addListener(
        new ImageStreamListener((ImageInfo info, bool _) {
          width = info.image.width;
          print('height===$width');
          completer.complete(width);
        }));
    return completer.future;
  }

  /*获取本地图片高度
  * */
  getLocalImageHeight(String path){
    int height;
    Completer<int> completer = new Completer<int>();
    Image image = Image.file(File.fromUri(Uri.parse(path)));
    // 预先获取图片信息
    image.image.resolve(new ImageConfiguration()).addListener(
        new ImageStreamListener((ImageInfo info, bool _) {
          height = info.image.height;
          print('height===$height');
          completer.complete(height);
        }));
    return completer.future;
  }
//使用
int originalWidth,originalHeight;
originalWidth =  await Utils().getLocalImageWidth(imagePath);
originalHeight = await Utils().getLocalImageHeight(imagePath);

///======
//加载file类型的图片
return Image.file(imageFile,
           width: 200.w,
           height: 200.w,
           fit: BoxFit.fill,
           frameBuilder: (context, child, frame, wasSynchronouslyLoaded){
           		//加载完成,显示图片
               if(wasSynchronouslyLoaded){
                    return child;
                    }
                    //加载中,添加透明渐变
                    return AnimatedOpacity(
                            child: child,
                              opacity: frame == null ? 0 : 1,
                              duration: const Duration(seconds: 4),
                              curve: Curves.easeOut,
                       );
                       }),

//加载Uint8List类型的图片
return Image.memory(imageUint8List);
//如果得到的图片是base64加密的字符串,将字符串反编译出Uint8List数组后再加载
Image.memory(Base64Decoder().convert(slideImageBase64)));


你可能感兴趣的:(flutter,android)