yekeskin / flutter_avif

A flutter plugin to view and encode avif images.
https://pub.dev/packages/flutter_avif
MIT License
61 stars 17 forks source link

Custom Paint #52

Closed zhalil closed 4 months ago

zhalil commented 4 months ago

Hello, is it possible to use avif in a custom paint widget somehow?

yekeskin commented 4 months ago

You can use decodeAvif to get an instance of a dart:ui Image

final bytes = await rootBundle.load('assets/test.avif');
final frames = await decodeAvif(bytes.buffer.asUint8List());
final avifImage = frames[0].image;

and draw it with drawImage in your custom painter.

import 'dart:ui' as ui;

class ImagePainter extends CustomPainter {
  ui.Image? image;

  ImagePainter(this.image);

  @override
  void paint(Canvas canvas, Size size) {
    if (image != null) {
      canvas.drawImage(image!, Offset.zero, Paint());
    }
  }

  @override
  bool shouldRepaint(ImagePainter oldDelegate) {
    return oldDelegate.image != image;
  }
}
CustomPaint(
  painter: ImagePainter(avifImage),
),