Created
April 5, 2024 20:16
-
-
Save mikkomcmenamin/faa74cb31fa16fe02a8e56743fbfaee0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
class InnerShadow extends SingleChildRenderObjectWidget { | |
const InnerShadow({ | |
super.key, | |
this.shadows = const <Shadow>[], | |
super.child, | |
}); | |
final List<Shadow> shadows; | |
@override | |
RenderObject createRenderObject(BuildContext context) { | |
final renderObject = _RenderInnerShadow(); | |
updateRenderObject(context, renderObject); | |
return renderObject; | |
} | |
@override | |
void updateRenderObject(BuildContext context, _RenderInnerShadow renderObject) { | |
renderObject.shadows = shadows; | |
} | |
} | |
class _RenderInnerShadow extends RenderProxyBox { | |
late List<Shadow> shadows; | |
@override | |
void paint(PaintingContext context, Offset offset) { | |
if (child == null) return; | |
final bounds = offset & size; | |
context.canvas.saveLayer(bounds, Paint()); | |
context.paintChild(child!, offset); | |
for (final shadow in shadows) { | |
final shadowRect = bounds.inflate(shadow.blurSigma); | |
final shadowPaint = Paint() | |
..blendMode = BlendMode.srcATop | |
..colorFilter = ColorFilter.mode(shadow.color, BlendMode.srcOut) | |
..imageFilter = ImageFilter.blur(sigmaX: shadow.blurSigma, sigmaY: shadow.blurSigma); | |
context.canvas | |
..saveLayer(shadowRect, shadowPaint) | |
..translate(shadow.offset.dx, shadow.offset.dy); | |
context.paintChild(child!, offset); | |
context.canvas.restore(); | |
} | |
context.canvas.restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment