Created
July 29, 2020 02:55
-
-
Save jaysonss/cf4a9760c2cb484d2a1badbe8aed34f9 to your computer and use it in GitHub Desktop.
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'; | |
///Inset Shadow | |
class InsetShadow extends SingleChildRenderObjectWidget { | |
const InsetShadow({ | |
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 { | |
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