cpdf_image_slider_thumb_shape.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'dart:ui' as ui;
  4. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. ///
  6. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. /// This notice may not be removed from this file.
  10. class CPDFImageSliderThumbShape extends SliderComponentShape {
  11. final double imageWidth;
  12. final double imageHeight;
  13. final ui.Image image;
  14. CPDFImageSliderThumbShape({
  15. this.imageWidth = 20,
  16. this.imageHeight = 20,
  17. required this.image,
  18. });
  19. @override
  20. Size getPreferredSize(bool isEnabled, bool isDiscrete) {
  21. return Size(imageWidth, imageHeight);
  22. }
  23. @override
  24. void paint(
  25. PaintingContext context,
  26. Offset center, {
  27. Animation<double>? activationAnimation,
  28. Animation<double>? enableAnimation,
  29. bool? isDiscrete,
  30. TextPainter? labelPainter,
  31. RenderBox? parentBox,
  32. SliderThemeData? sliderTheme,
  33. TextDirection? textDirection,
  34. double? value,
  35. double? textScaleFactor,
  36. Size? sizeWithOverflow,
  37. }) {
  38. final canvas = context.canvas;
  39. final imageOffset =
  40. Offset(center.dx - imageWidth / 2, center.dy - imageHeight / 2);
  41. // 计算缩放比例
  42. final double targetWidth = imageWidth; // 目标宽度
  43. final double targetHeight = imageHeight; // 目标高度
  44. final double scaleX = targetWidth / image.width.toDouble();
  45. final double scaleY = targetHeight / image.height.toDouble();
  46. final double scale = scaleX < scaleY ? scaleX : scaleY;
  47. // 计算缩放后的图片尺寸
  48. final double destWidth = image.width.toDouble() * scale;
  49. final double destHeight = image.height.toDouble() * scale;
  50. // canvas.translate(imageOffset.dx, imageOffset.dy);
  51. // canvas.rotate(1.6);
  52. // 绘制图片,并指定位置和大小
  53. final paint = Paint();
  54. final srcRect = Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble());
  55. final dstRect = Rect.fromLTWH(imageOffset.dx, imageOffset.dy, destWidth, destHeight);
  56. canvas.drawImageRect(image, srcRect, dstRect, paint);
  57. }
  58. }