3

In Flutter, How to get the width and height of a imageProvider?

In the example below, I need to get the width of the imageProvider. So that I can calculate the minScale for the PhotoView widget in photo_view package.

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

class ImageViewerScreen extends StatelessWidget {

  final ImageProvider imageProvider;

  ImageViewerScreen({@required this.imageProvider});

  @override
  Widget build(BuildContext context) {
    final double _screenWidth = MediaQuery.of(context).size.width;
    final double _imageWidth =
        imageProvider.width; //Here is the question: how to get the width of this imageProvider?
    final double _minScale = _screenWidth / _imageWidth;

    return Scaffold(
      appBar: AppBar(),
      body: Container(
          child: PhotoView(
        imageProvider: imageProvider,
        minScale: _minScale,
      )),
    );
  }
}
dreamkast
  • 511
  • 1
  • 4
  • 10
  • Does [this](https://stackoverflow.com/questions/44665955/how-do-i-determine-the-width-and-height-of-an-image-in-flutter) help? – tomerpacific May 24 '20 at 08:58
  • 1
    Thank you so much guys! I have also read that post before asked the question, but found the solution may be unreasonably complicated and expensive (involved future, listener, FutureBuilder...) to just get the width of an image. I just wonder whether there is some simpler way to do it. Seems that such simpler method not yet existed. Hope that flutter team will provide it soon. Thank you for your suggestion again. – dreamkast May 24 '20 at 17:30

1 Answers1

0

For now, there is an existing Flutter plugins that you can use. This is not available during the time that you've posted the question. I suggest to check the image_size_getter, which is now available in pub.dev. As an overview, this package helps on reading the metadata to get the width and height but doesn't completely decode the image file itself.

Supported formats are:

  • jpeg
  • gif
  • png
  • webp
  • bmp
MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65