如何确定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<ui.Image> completer = new Completer<ui.Image>();
image.image.resolve(ImageConfiguration()).addListener((ImageInfo image, bool synchronousCall){
completer.complete(image.image);
});
return ListView(children: <Widget>[
new FutureBuilder<ui.Image>(
future: completer.future,
builder: (BuildContext context, AsyncSnapshot<ui.Image> 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,
],);
}
}
效果如下