Either drawImage or paintImage doesn't work properly in Flutter

别来无恙 提交于 2019-11-30 16:35:26

There are two classes called Image in flutter, in different packages.

There's the Widget, which behaves like a widget and can be instantiated from an asset, from memory or from the network through its named constructors.

There's also the ui package Image which is used when painting at a lower level, for example in a CustomPainter. Since this version is in the ui package it's normally imported with the ui prefix like this:

import 'dart:ui' as ui;

Don't import material as ui! That will lead to a lot of confusion.

To make a widget, use the Image.asset constructor, passing the asset name.

To make a ui.Image from an asset use this snippet:

  Future<ui.Image> load(String asset) async {
    ByteData data = await rootBundle.load(asset);
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
    ui.FrameInfo fi = await codec.getNextFrame();
    return fi.image;
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!