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

如何确定Flutter中图像的宽度和高度?,第1张

如何确定Flutter中图像的宽度和高度? 更新的解决方案

随着新版本的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,        ],      ),    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4910821.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存