Skip to content

Instantly share code, notes, and snippets.

@hnvn
Last active June 23, 2018 04:26
Show Gist options
  • Save hnvn/a23d7294d5a795217b2f4008d2c56e72 to your computer and use it in GitHub Desktop.
Save hnvn/a23d7294d5a795217b2f4008d2c56e72 to your computer and use it in GitHub Desktop.
import 'package:flutter/rendering.dart';
class _Shimmer extends SingleChildRenderObjectWidget {
final Gradient gradient;
_Shimmer({Widget child, this.gradient})
: super(child: child);
@override
_ShimmerFilter createRenderObject(BuildContext context) {
return _ShimmerFilter(gradient);
}
}
class _ShimmerFilter extends RenderProxyBox {
final _clearPaint = Paint();
final Paint _gradientPaint;
final Gradient _gradient;
_ShimmerFilter(this._gradient)
: _gradientPaint = Paint()..blendMode = BlendMode.srcIn;
@override
bool get alwaysNeedsCompositing => child != null;
@override
void paint(PaintingContext context, Offset offset) {
if (child != null) {
assert(needsCompositing);
final rect = offset & child.size;
_gradientPaint.shader = _gradient.createShader(rect);
context.canvas.saveLayer(rect, _clearPaint);
context.paintChild(child, offset);
context.canvas.drawRect(rect, _gradientPaint);
context.canvas.restore();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment