随着新版本的flutter,旧解决方案已过时。现在
addListener需要
ImageStreamListener。
Widget build(BuildContext context) { Image image = new Image.network('https://i.stack.imgur.com/lkd0a.png'); Completer<ui.Image> completer = new Completer<ui.Image>(); image.image .resolve(new ImageConfiguration()) .addListener(ImageStreamListener(ImageInfo info, bool _) { completer.complete(info.image)); }) ... ...
原始版本:
如果您已经有一个
Image小部件,则可以
ImageStream通过调用它来读取
resolve它的内容
ImageProvider。
import 'dart:ui' as ui;import 'dart:async';import 'package:flutter/material.dart';import 'package:flutter/services.dart';void main() { runApp(new MaterialApp( home: new MyHomePage(), ));}class MyHomePage extends StatelessWidget { Widget build(BuildContext context) { Image image = new Image.network('https://i.stack.imgur.com/lkd0a.png'); Completer<ui.Image> completer = new Completer<ui.Image>(); image.image .resolve(new ImageConfiguration()) .addListener((ImageInfo info, bool _) => completer.complete(info.image)); return new Scaffold( appBar: new AppBar( title: new Text("Image Dimensions Example"), ), body: new ListView( children: [ 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, ], ), ); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)