美文网首页flutter相关
Flutter获取图片大小(网络图片和本地图片)

Flutter获取图片大小(网络图片和本地图片)

作者: 下雨不愁_b1b5 | 来源:发表于2019-12-22 22:26 被阅读0次
    WechatIMG32.png

    可以获取图片的大小

    使用

    本地资源使用:

      AsperctRaioImage.asset(
                'images/img_home_bg.jpg',
                builder: (context, snapshot, url) {
                  print('width=${snapshot.data.width}');
                  print('heiht=${snapshot.data.height}');
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('本地资源加载',style: TextStyle(fontSize: 25.0,color: Colors.black),),
                      Text('大小--${snapshot.data.width.toDouble()}x${snapshot.data.height.toDouble()}',style: TextStyle(fontSize: 17.0),),
                      Container(
                        width: snapshot.data.width.toDouble() / 5,
                        height: snapshot.data.height.toDouble() / 5,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                              image: AssetImage(url), fit: BoxFit.cover),
                        ),
                      )
                    ],
                  );
                },),
    

    网络资源使用:

    AsperctRaioImage.network(
                  'http://g.hiphotos.baidu.com/image/pic/item/c2cec3fdfc03924590b2a9b58d94a4c27d1e2500.jpg',
                  builder: (context, snapshot, url) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('网络图片加载',style: TextStyle(fontSize: 25.0,color: Colors.black),),
                        Text('大小--${snapshot.data.width.toDouble()}x${snapshot.data.height.toDouble()}',style: TextStyle(fontSize: 17.0),),
                        Container(
                          width: snapshot.data.width.toDouble() / 5,
                          height: snapshot.data.height.toDouble() / 5,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                                image: NetworkImage(url), fit: BoxFit.cover),
                          ),
                        )
                      ],
                    );
                  })
    

    AsperctRaioImage工具类

    import 'dart:async';
    import 'dart:io';
    import 'dart:typed_data';
    import 'package:flutter/material.dart';
    import 'dart:ui' as ui;
    
    typedef AsyncImageWidgetBuilder<T> = Widget Function(
        BuildContext context, AsyncSnapshot<T> snapshot, String url);
    
    typedef AsyncImageFileWidgetBuilder<T> = Widget Function(
        BuildContext context, AsyncSnapshot<T> snapshot, File file);
    
    typedef AsyncImageMemoryWidgetBuilder<T> = Widget Function(
        BuildContext context, AsyncSnapshot<T> snapshot, Uint8List bytes);
    
    enum AsperctRaioImageType { NETWORK, FILE, ASSET, MEMORY }
    
    ///有宽高的Image
    class AsperctRaioImage extends StatelessWidget {
      String url;
      File file;
      Uint8List bytes;
      final ImageProvider provider;
      AsperctRaioImageType type;
      AsyncImageWidgetBuilder<ui.Image> builder;
      AsyncImageFileWidgetBuilder<ui.Image> filebBuilder;
      AsyncImageMemoryWidgetBuilder<ui.Image> memoryBuilder;
    
      AsperctRaioImage.network(url, {Key key, @required this.builder})
          : provider = NetworkImage(url),
            type = AsperctRaioImageType.NETWORK,
            this.url = url;
    
      AsperctRaioImage.file(
        file, {
        Key key,
        @required this.filebBuilder,
      })  : provider = FileImage(file),
            type = AsperctRaioImageType.FILE,
            this.file = file;
    
      AsperctRaioImage.asset(name, {Key key, @required this.builder})
          : provider = AssetImage(name),
            type = AsperctRaioImageType.ASSET,
            this.url = name;
    
      AsperctRaioImage.memory(bytes, {Key key, @required this.memoryBuilder})
          : provider = MemoryImage(bytes),
            type = AsperctRaioImageType.MEMORY,
            this.bytes = bytes;
    
      @override
      Widget build(BuildContext context) {
        final ImageConfiguration config = createLocalImageConfiguration(context);
        final Completer<ui.Image> completer = Completer<ui.Image>();
        final ImageStream stream = provider.resolve(config);
        ImageStreamListener listener;
        listener = ImageStreamListener(
              (ImageInfo image, bool sync) {
            completer.complete(image.image);
            stream.removeListener(listener);
          },
          onError: (dynamic exception, StackTrace stackTrace) {
            completer.complete();
            stream.removeListener(listener);
              FlutterError.reportError(FlutterErrorDetails(
                context: ErrorDescription('image failed to precache'),
                library: 'image resource service',
                exception: exception,
                stack: stackTrace,
                silent: true,
              ));
          },
        );
        stream.addListener(listener);
    
        return FutureBuilder(
            future: completer.future,
            builder: (BuildContext context, AsyncSnapshot<ui.Image> snapshot) {
              if (snapshot.hasData) {
                if (type == AsperctRaioImageType.FILE) {
                  return filebBuilder(context, snapshot, file);
                } else if (type == AsperctRaioImageType.MEMORY) {
                  return memoryBuilder(context, snapshot, bytes);
                } else {
                  return builder(context, snapshot, url);
                }
              } else {
                return Container();
              }
            });
      }
    }
    

    相关文章

      网友评论

        本文标题:Flutter获取图片大小(网络图片和本地图片)

        本文链接:https://www.haomeiwen.com/subject/madanctx.html