makeImage method

Future makeImage(
  1. int cols,
  2. int rows
)

if !isWeb returns ui.Image else an Image

Implementation

Future<dynamic> makeImage(int cols, int rows) {
  final c = Completer<dynamic>();
  Uint32List pixels = Uint32List(rows * cols);
  int i = 0;
  colors.forEach((row) {
    row.forEach((col) {
      // Color.value is in abgr8888 format
      // Must be converted to rgba8888
      pixels[i] = (((col.alpha & 0xff) << 24) |
              ((col.blue & 0xff) << 16) |
              ((col.green & 0xff) << 8) |
              ((col.red & 0xff) << 0)) &
          0xFFFFFFFF;
      i++;
    });
  });

  // this doesn't work on web: https://github.com/flutter/flutter/issues/45190
  // it never complete
  // Use this on other platforms because it's faster
  if (!kIsWeb)
    ui.decodeImageFromPixels(
      pixels.buffer.asUint8List(),
      cols,
      rows,
      ui.PixelFormat.rgba8888,
      c.complete,
    );
  else {
    // so use decodeImageFromList which need an encoded image
    final header = RGBA32BitmapHeader(cols * rows * 4, cols, rows)
      ..applyContent(pixels.buffer.asUint8List());

    ui.decodeImageFromList(header.headerIntList, (ui.Image img) {
      c.complete(img);
    });
  }

  return c.future;
}