clip_shadow.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. library clip_shadow;
  2. import 'package:flutter/widgets.dart';
  3. class _ClipShadowPainter extends CustomPainter {
  4. /// If non-null, determines which clip to use.
  5. final CustomClipper<Path> clipper;
  6. /// A list of shadows cast by this box behind the box.
  7. final List<BoxShadow> clipShadow;
  8. _ClipShadowPainter({
  9. required this.clipper,
  10. required this.clipShadow
  11. });
  12. @override
  13. void paint(Canvas canvas, Size size) {
  14. clipShadow.forEach((BoxShadow shadow) {
  15. var paint = shadow.toPaint();
  16. var spreadSize = Size(size.width + shadow.spreadRadius * 2, size.height + shadow.spreadRadius * 2);
  17. var clipPath = clipper.getClip(spreadSize).shift(Offset(
  18. shadow.offset.dx - shadow.spreadRadius,
  19. shadow.offset.dy - shadow.spreadRadius
  20. ));
  21. canvas.drawPath(clipPath, paint);
  22. // canvas.drawShadow(clipper.getClip(size), shadow.color, shadow.spreadRadius, true);
  23. });
  24. }
  25. @override
  26. bool shouldRepaint(CustomPainter oldDelegate) {
  27. return true;
  28. }
  29. }
  30. class ClipShadow extends StatelessWidget {
  31. /// A list of shadows cast by this box behind the box.
  32. final List<BoxShadow> boxShadow;
  33. /// If non-null, determines which clip to use.
  34. final CustomClipper<Path> clipper;
  35. /// The [Widget] below this widget in the tree.
  36. final Widget child;
  37. ClipShadow({
  38. required this.boxShadow,
  39. required this.clipper,
  40. required this.child
  41. });
  42. @override
  43. Widget build(BuildContext context) {
  44. return CustomPaint(
  45. painter: _ClipShadowPainter(
  46. clipShadow: boxShadow,
  47. clipper: clipper
  48. ),
  49. child: ClipPath(
  50. clipper: clipper,
  51. child: child,
  52. ),
  53. );
  54. }
  55. }