Line data Source code
1 : import 'dart:math';
2 : import 'dart:ui';
3 :
4 : import 'package:flutter/material.dart';
5 :
6 : @immutable
7 : class CircularRevealClipper extends CustomClipper<Path> {
8 : final double fraction;
9 : final Alignment? centerAlignment;
10 : final Offset? centerOffset;
11 : final double? minRadius;
12 : final double? maxRadius;
13 :
14 1 : const CircularRevealClipper({
15 : required this.fraction,
16 : this.centerAlignment,
17 : this.centerOffset,
18 : this.minRadius,
19 : this.maxRadius,
20 : });
21 :
22 1 : @override
23 : Path getClip(Size size) {
24 2 : final Offset center = centerAlignment?.alongSize(size) ??
25 0 : centerOffset ??
26 0 : Offset(size.width / 2, size.height / 2);
27 1 : final minRadius = this.minRadius ?? 0;
28 1 : final maxRadius = this.maxRadius ?? calcMaxRadius(size, center);
29 :
30 1 : return Path()
31 1 : ..addOval(
32 1 : Rect.fromCircle(
33 : center: center,
34 2 : radius: lerpDouble(minRadius, maxRadius, fraction)!,
35 : ),
36 : );
37 : }
38 :
39 1 : @override
40 : bool shouldReclip(CustomClipper<Path> oldClipper) => true;
41 :
42 0 : static double calcMaxRadius(Size size, Offset center) {
43 0 : final w = max(center.dx, size.width - center.dx);
44 0 : final h = max(center.dy, size.height - center.dy);
45 0 : return sqrt(w * w + h * h);
46 : }
47 : }
|