如何确定Flutter中网络图像的宽度和高度

如何确定Flutter中网络图像的宽度和高度

  • 如果已有Image小部件,则可以通过在其ImageProvider上调用resolve来读取ImageStream
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'dart:async';

class TestImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Image image = Image.network('http://pic75.nipic.com/file/20150821/9448607_145742365000_2.jpg');
    Completer completer = new Completer();
    image.image.resolve(ImageConfiguration()).addListener((ImageInfo image, bool synchronousCall){
      completer.complete(image.image);
    });
    return ListView(children: [
      new FutureBuilder(
        future: completer.future,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return new Text(
              '${snapshot.data.width}x${snapshot.data.height}',
              style: Theme.of(context).textTheme.display3,
            );
          } else {
            return new Text('Loading...');
          }
        },
      ),
      image,
    ],);
  }
}

效果如下

如何确定Flutter中网络图像的宽度和高度_第1张图片
获取宽高

你可能感兴趣的:(如何确定Flutter中网络图像的宽度和高度)