Flutter獲取圖片大小(網(wǎng)絡(luò)圖片和本地圖片)

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),
                    ),
                  )
                ],
              );
            },),

網(wǎng)絡(luò)資源使用:

AsperctRaioImage.network(
              'http://g.hiphotos.baidu.com/image/pic/item/c2cec3fdfc03924590b2a9b58d94a4c27d1e2500.jpg',
              builder: (context, snapshot, url) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('網(wǎng)絡(luò)圖片加載',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();
          }
        });
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,626評(píng)論 1 32
  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,283評(píng)論 0 5
  • 下午一個(gè)聊的很好的同事說(shuō)漏嘴,半年之內(nèi)會(huì)跳槽。當(dāng)時(shí)心里真的很有沖擊感,離別總要習(xí)慣才行呀 自己都一年年考研要走,又...
    Bparanea閱讀 302評(píng)論 0 0
  • 閱讀1小時(shí),總計(jì)657小時(shí),第621日。 閱讀《利維坦》至13% 在“利維坦”中,“主權(quán)”是使整體得到生命和活動(dòng)的...
    龍?zhí)赘缢_克海龍閱讀 438評(píng)論 0 0
  • 內(nèi)存分析 靜態(tài)分析 Analyze 動(dòng)態(tài)分析 Instruments 可以查看內(nèi)存分配情況Allocations ...
    ChancePro閱讀 110評(píng)論 0 0

友情鏈接更多精彩內(nèi)容