|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'dart:math' as math; |
| 3 | + |
| 4 | +class WaveWarpEffect extends StatefulWidget { |
| 5 | + final Widget child; |
| 6 | + final double waveHeight; |
| 7 | + final double waveWidth; |
| 8 | + final double speed; |
| 9 | + final double phase; |
| 10 | + |
| 11 | + const WaveWarpEffect({ |
| 12 | + Key? key, |
| 13 | + required this.child, |
| 14 | + this.waveHeight = 20.0, |
| 15 | + this.waveWidth = 100.0, |
| 16 | + this.speed = 1.0, |
| 17 | + this.phase = 0.0, |
| 18 | + }) : super(key: key); |
| 19 | + |
| 20 | + @override |
| 21 | + _WaveWarpEffectState createState() => _WaveWarpEffectState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _WaveWarpEffectState extends State<WaveWarpEffect> |
| 25 | + with SingleTickerProviderStateMixin { |
| 26 | + late AnimationController _controller; |
| 27 | + |
| 28 | + @override |
| 29 | + void initState() { |
| 30 | + super.initState(); |
| 31 | + _controller = AnimationController( |
| 32 | + vsync: this, |
| 33 | + duration: Duration(seconds: (5 / widget.speed).round()), |
| 34 | + )..repeat(); |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + void dispose() { |
| 39 | + _controller.dispose(); |
| 40 | + super.dispose(); |
| 41 | + } |
| 42 | + |
| 43 | + @override |
| 44 | + Widget build(BuildContext context) { |
| 45 | + return AnimatedBuilder( |
| 46 | + animation: _controller, |
| 47 | + builder: (context, child) { |
| 48 | + return ClipPath( |
| 49 | + clipper: WaveClipper( |
| 50 | + animationValue: _controller.value, |
| 51 | + waveHeight: widget.waveHeight, |
| 52 | + waveWidth: widget.waveWidth, |
| 53 | + phase: widget.phase, |
| 54 | + ), |
| 55 | + child: widget.child, |
| 56 | + ); |
| 57 | + }, |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class WaveClipper extends CustomClipper<Path> { |
| 63 | + final double animationValue; |
| 64 | + final double waveHeight; |
| 65 | + final double waveWidth; |
| 66 | + final double phase; |
| 67 | + |
| 68 | + WaveClipper({ |
| 69 | + required this.animationValue, |
| 70 | + required this.waveHeight, |
| 71 | + required this.waveWidth, |
| 72 | + required this.phase, |
| 73 | + }); |
| 74 | + |
| 75 | + @override |
| 76 | + Path getClip(Size size) { |
| 77 | + final path = Path(); |
| 78 | + path.moveTo(0, 0); |
| 79 | + |
| 80 | + // Create wave effect |
| 81 | + for (double x = 0; x < size.width; x++) { |
| 82 | + final double y = waveHeight * |
| 83 | + math.sin((x / waveWidth) * 2 * math.pi + |
| 84 | + (animationValue * 2 * math.pi) + |
| 85 | + phase); |
| 86 | + |
| 87 | + path.lineTo(x, y); |
| 88 | + } |
| 89 | + |
| 90 | + // Complete the path |
| 91 | + path.lineTo(size.width, size.height); |
| 92 | + path.lineTo(0, size.height); |
| 93 | + path.close(); |
| 94 | + |
| 95 | + return path; |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + bool shouldReclip(WaveClipper oldClipper) => |
| 100 | + animationValue != oldClipper.animationValue; |
| 101 | +} |
| 102 | + |
| 103 | +// Usage Example: |
| 104 | +class WaveWarpDemo extends StatelessWidget { |
| 105 | + @override |
| 106 | + Widget build(BuildContext context) { |
| 107 | + return Scaffold( |
| 108 | + body: Center( |
| 109 | + child: WaveWarpEffect( |
| 110 | + waveHeight: 20, |
| 111 | + waveWidth: 100, |
| 112 | + speed: 1.0, |
| 113 | + phase: 0, |
| 114 | + child: Container( |
| 115 | + width: 300, |
| 116 | + height: 200, |
| 117 | + color: Colors.blue, |
| 118 | + child: Center( |
| 119 | + child: Text( |
| 120 | + 'Wave Warp Effect', |
| 121 | + style: TextStyle(color: Colors.white), |
| 122 | + ), |
| 123 | + ), |
| 124 | + ), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments