|
| 1 | +import 'package:custom_refresh_indicator/custom_refresh_indicator.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +class EnvelopRefreshIndicator extends StatelessWidget { |
| 5 | + final Widget child; |
| 6 | + final bool leadingScrollIndicatorVisible; |
| 7 | + final bool trailingScrollIndicatorVisible; |
| 8 | + final RefreshCallback onRefresh; |
| 9 | + final Color? accent; |
| 10 | + |
| 11 | + static const _circleSize = 70.0; |
| 12 | + |
| 13 | + static const _defaultShadow = [ |
| 14 | + BoxShadow(blurRadius: 10, color: Colors.black26) |
| 15 | + ]; |
| 16 | + |
| 17 | + const EnvelopRefreshIndicator({ |
| 18 | + Key? key, |
| 19 | + required this.child, |
| 20 | + required this.onRefresh, |
| 21 | + this.leadingScrollIndicatorVisible = false, |
| 22 | + this.trailingScrollIndicatorVisible = true, |
| 23 | + this.accent, |
| 24 | + }) : super(key: key); |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return CustomRefreshIndicator( |
| 29 | + leadingScrollIndicatorVisible: leadingScrollIndicatorVisible, |
| 30 | + trailingScrollIndicatorVisible: trailingScrollIndicatorVisible, |
| 31 | + builder: (context, child, controller) => |
| 32 | + LayoutBuilder(builder: (context, constraints) { |
| 33 | + return AnimatedBuilder( |
| 34 | + animation: controller, |
| 35 | + builder: (context, _) { |
| 36 | + final widgetWidth = constraints.maxWidth; |
| 37 | + final widgetHeight = constraints.maxHeight; |
| 38 | + final letterTopWidth = (widgetWidth / 2) + 50; |
| 39 | + |
| 40 | + final leftValue = |
| 41 | + (widgetWidth - (letterTopWidth * controller.value / 1)) |
| 42 | + .clamp(letterTopWidth - 100, double.infinity); |
| 43 | + |
| 44 | + final rightValue = |
| 45 | + (widgetWidth - (widgetWidth * controller.value / 1)) |
| 46 | + .clamp(0.0, double.infinity); |
| 47 | + |
| 48 | + final opacity = (controller.value - 1).clamp(0, 0.5) / 0.5; |
| 49 | + return Stack( |
| 50 | + children: <Widget>[ |
| 51 | + Transform.scale( |
| 52 | + scale: 1 - 0.1 * controller.value.clamp(0.0, 1.0), |
| 53 | + child: child, |
| 54 | + ), |
| 55 | + Positioned( |
| 56 | + right: rightValue, |
| 57 | + child: Container( |
| 58 | + height: widgetHeight, |
| 59 | + width: widgetWidth, |
| 60 | + decoration: const BoxDecoration( |
| 61 | + color: Colors.white, |
| 62 | + boxShadow: _defaultShadow, |
| 63 | + ), |
| 64 | + ), |
| 65 | + ), |
| 66 | + Positioned( |
| 67 | + left: leftValue, |
| 68 | + child: CustomPaint( |
| 69 | + painter: TrianglePainter( |
| 70 | + strokeColor: Colors.white, |
| 71 | + paintingStyle: PaintingStyle.fill, |
| 72 | + ), |
| 73 | + child: SizedBox( |
| 74 | + height: widgetHeight, |
| 75 | + width: letterTopWidth, |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + if (controller.value >= 1) |
| 80 | + Container( |
| 81 | + padding: const EdgeInsets.only(right: 100), |
| 82 | + child: Transform.scale( |
| 83 | + scale: controller.value, |
| 84 | + child: Opacity( |
| 85 | + opacity: controller.isLoading ? 1 : opacity, |
| 86 | + child: Align( |
| 87 | + alignment: Alignment.center, |
| 88 | + child: Container( |
| 89 | + width: _circleSize, |
| 90 | + height: _circleSize, |
| 91 | + decoration: BoxDecoration( |
| 92 | + boxShadow: _defaultShadow, |
| 93 | + color: accent ?? |
| 94 | + Theme.of(context).colorScheme.primary, |
| 95 | + shape: BoxShape.circle, |
| 96 | + ), |
| 97 | + child: Stack( |
| 98 | + alignment: Alignment.center, |
| 99 | + children: <Widget>[ |
| 100 | + SizedBox( |
| 101 | + height: double.infinity, |
| 102 | + width: double.infinity, |
| 103 | + child: CircularProgressIndicator( |
| 104 | + valueColor: const AlwaysStoppedAnimation( |
| 105 | + Colors.black), |
| 106 | + value: controller.isLoading ? null : 0, |
| 107 | + ), |
| 108 | + ), |
| 109 | + const Icon( |
| 110 | + Icons.mail_outline, |
| 111 | + color: Colors.white, |
| 112 | + size: 35, |
| 113 | + ), |
| 114 | + ], |
| 115 | + ), |
| 116 | + ), |
| 117 | + ), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ) |
| 121 | + ], |
| 122 | + ); |
| 123 | + }); |
| 124 | + }), |
| 125 | + child: child, |
| 126 | + onRefresh: () => Future<void>.delayed(const Duration(seconds: 2)), |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +class TrianglePainter extends CustomPainter { |
| 132 | + final Color strokeColor; |
| 133 | + final PaintingStyle paintingStyle; |
| 134 | + final double strokeWidth; |
| 135 | + |
| 136 | + static double convertRadiusToSigma(double radius) { |
| 137 | + return radius * 0.57735 + 0.5; |
| 138 | + } |
| 139 | + |
| 140 | + TrianglePainter( |
| 141 | + {this.strokeColor = Colors.black, |
| 142 | + this.strokeWidth = 3, |
| 143 | + this.paintingStyle = PaintingStyle.stroke}); |
| 144 | + |
| 145 | + @override |
| 146 | + void paint(Canvas canvas, Size size) { |
| 147 | + Paint paint = Paint() |
| 148 | + ..color = strokeColor |
| 149 | + ..strokeWidth = strokeWidth |
| 150 | + ..style = paintingStyle; |
| 151 | + final path = getTrianglePath(size.width, size.height); |
| 152 | + final shadowPaint = Paint() |
| 153 | + ..color = Colors.black.withAlpha(50) |
| 154 | + ..maskFilter = |
| 155 | + MaskFilter.blur(BlurStyle.normal, convertRadiusToSigma(10)); |
| 156 | + canvas.drawPath(path, shadowPaint); |
| 157 | + |
| 158 | + canvas.drawPath(path, paint); |
| 159 | + } |
| 160 | + |
| 161 | + Path getTrianglePath(double x, double y) { |
| 162 | + return Path() |
| 163 | + ..moveTo(0, y / 2) |
| 164 | + ..lineTo(x, 0) |
| 165 | + ..lineTo(x, y) |
| 166 | + ..lineTo(0, y / 2); |
| 167 | + } |
| 168 | + |
| 169 | + @override |
| 170 | + bool shouldRepaint(TrianglePainter oldDelegate) { |
| 171 | + return oldDelegate.strokeColor != strokeColor || |
| 172 | + oldDelegate.paintingStyle != paintingStyle || |
| 173 | + oldDelegate.strokeWidth != strokeWidth; |
| 174 | + } |
| 175 | +} |
0 commit comments