import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class VidPainter extends CustomPainter {
  VidPainter({required this.image, required this.contextSize});
  ui.Image image;
  Size contextSize;

  @override
  void paint(Canvas canvas, Size size) async {
    final double scale = size.width / image.width;
    final double offsetY = (size.height - image.height * scale) / 2;

    // print("${DateTime.now()}容器大小 ContextSize = $contextSize");
    // print("${DateTime.now()}图片大小 ImageSize = ${image.width}x${image.height}");
    // print("${DateTime.now()}画布大小 CanvasSize = $size");
    // print("${DateTime.now()}缩放比例 Scale = $scale");
    // print(
    //     "${DateTime.now()}缩放后宽高 ScaledImageSize = ${image.width * scale}x${image.height * scale}");
    // print("${DateTime.now()}偏移量 Offset = $offsetY");

    Paint paint = Paint();
    canvas.save();
    // canvas.scale(scale);
    // canvas.drawImage(image, Offset(0, offsetY), paint);
    canvas.drawImageRect(
        image,
        Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
        Rect.fromLTWH(0, offsetY, size.width, image.height * scale),
        paint);

    canvas.restore();
    image.dispose();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
