Created
March 17, 2022 20:32
-
-
Save Chappie74/718bfc917196af03f87247731669bb14 to your computer and use it in GitHub Desktop.
Flutter Inner Shadow
This file contains 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({ | |
Key? key, | |
this.shadows = const <Shadow>[], | |
Widget? child, | |
}) : super(key: key, child: 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