How do I determine the width and height of an image in Flutter?

后端 未结 5 1477
傲寒
傲寒 2020-11-27 15:00

Assume I have declared my image in my pubspec.yaml like this:

  assets:
    - assets/kitten.jpg

And my Flutter code is this:



        
相关标签:
5条回答
  • 2020-11-27 15:30

    With new version of flutter old solution not working example:

      image.image
      .resolve(new ImageConfiguration())
      .addListener((ImageInfo info, bool _) => completer.complete(info.image));
    

    Below the Working version:

    _image.image
        .resolve(new ImageConfiguration())
        .addListener(new ImageStreamListener((ImageInfo image, bool _) {
      completer.complete(image.image);
    }));
    
    0 讨论(0)
  • 2020-11-27 15:31

    You can resolve the ImageProvider to get an ImageStream, then use addListener to be notified when the image is ready.

    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 {
    
      Future<ui.Image> _getImage() {
        Completer<ui.Image> completer = new Completer<ui.Image>();
        new NetworkImage('https://i.stack.imgur.com/lkd0a.png')
          .resolve(new ImageConfiguration())
          .addListener((ImageInfo info, bool _) => completer.complete(info.image));
        return completer.future;
      }
    
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Image Dimensions Example"),
          ),
          body: new Center(
            child: new FutureBuilder<ui.Image>(
              future: _getImage(),
              builder: (BuildContext context, AsyncSnapshot<ui.Image> snapshot) {
                if (snapshot.hasData) {
                  ui.Image image = snapshot.data;
                  return new Text(
                    '${image.width}x${image.height}',
                    style: Theme.of(context).textTheme.display4);
                } else {
                  return new Text('Loading...');
                }
              },
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-11-27 15:33

    Create a method, like:

    Future<Size> _calculateImageDimension() {
      Completer<Size> completer = Completer();
      Image image = Image.network("https://i.stack.imgur.com/lkd0a.png");
      image.image.resolve(ImageConfiguration()).addListener(
        ImageStreamListener(
          (ImageInfo image, bool synchronousCall) {
            var myImage = image.image;
            Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
            completer.complete(size);
          },
        ),
      );
      return completer.future;
    }
    

    And use it like:

    _calculateImageDimension().then((size) => print("size = ${size}")); // 487.0,696.0
    
    0 讨论(0)
  • 2020-11-27 15:37

    UPDATED SOLUTION:

    With the new version of flutter old solution is obsolete. Now the addListener needs an 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));
          })
        ...
        ...
    

    ORIGINAL VERSION:

    If you already have an Image widget, you can read the ImageStream out of it by calling resolve on its 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 讨论(0)
  • 2020-11-27 15:41

    The other answers seem overly complicated if you just want the width and height of an image in an async function. You can get the image resolution using flutter lib directly like this:

    import 'dart:io';
    
    File image = new File('image.png'); // Or any other way to get a File instance.
    var decodedImage = await decodeImageFromList(image.readAsBytesSync());
    print(decodedImage.width);
    print(decodedImage.height);
    
    0 讨论(0)
提交回复
热议问题