Line data Source code
1 : import 'package:flutter/cupertino.dart'; 2 : import 'package:flutter/material.dart'; 3 : 4 : import 'convex_shape.dart'; 5 : import 'reused_gradient.dart'; 6 : 7 : /// Custom painter to draw the [ConvexNotchedRectangle] into canvas. 8 : class ConvexPainter extends CustomPainter { 9 : final _paint = Paint(); 10 : final _shadowPaint = Paint(); 11 : final _shape = ConvexNotchedRectangle(); 12 : final ReusedGradient _gradient = ReusedGradient(); 13 : 14 : /// Width of the convex shape. 15 : final double width; 16 : 17 : /// Height of the convex shape. 18 : final double height; 19 : 20 : /// Position in vertical which describe the offset of shape. 21 : final double top; 22 : 23 : /// Position in horizontal which describe the offset of shape. 24 : final Animation<double> leftPercent; 25 : 26 : /// Create painter 27 1 : ConvexPainter({ 28 : this.top, 29 : this.width, 30 : this.height, 31 : this.leftPercent = const AlwaysStoppedAnimation<double>(0.5), 32 : Color color = Colors.white, 33 : Color shadowColor = Colors.black38, 34 : double sigma = 2, 35 : Gradient gradient, 36 1 : }) : super(repaint: leftPercent) { 37 2 : _paint..color = color; 38 1 : _shadowPaint 39 1 : ..color = shadowColor 40 2 : ..maskFilter = MaskFilter.blur(BlurStyle.outer, sigma); 41 2 : _gradient.gradient = gradient; 42 : } 43 : 44 1 : @override 45 : void paint(Canvas canvas, Size size) { 46 3 : Rect host = Rect.fromLTWH(0, 0, size.width, size.height); 47 1 : Rect guest = Rect.fromLTWH( 48 10 : size.width * leftPercent.value - width / 2, top, width, height); 49 3 : _gradient.updateWith(_paint, size: host); 50 2 : Path path = _shape.getOuterPath(host, guest); 51 2 : canvas.drawPath(path, _shadowPaint); 52 2 : canvas.drawPath(path, _paint); 53 : } 54 : 55 1 : @override 56 : bool shouldRepaint(ConvexPainter oldDelegate) { 57 5 : return oldDelegate.leftPercent.value != leftPercent.value || 58 3 : oldDelegate._paint != _paint; 59 : } 60 : }