Last active
January 9, 2024 07:05
-
-
Save aravindhkumar23/051cc638686008a966634f5282a9343c to your computer and use it in GitHub Desktop.
Clipping widgets with bezier curves in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//access the wavy header in body by below code | |
return Scaffold( | |
body: Container(height:200.0,child: new WavyHeader()), | |
); | |
//creates a curve with 3 curves merged by using stack | |
// ref link | |
https://iirokrankka.com/2017/09/04/clipping-widgets-with-bezier-curves-in-flutter/ | |
sample output | |
https://ibb.co/r7Q0B3k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class WavyHeader extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
children: <Widget>[ | |
ClipPath( | |
child: new Container( | |
color: Colors.blue[100], | |
), | |
clipper: BottomWaveClipper(), | |
), | |
ClipPath( | |
child: new Container( | |
color: Colors.blue[200].withOpacity(0.5), | |
), | |
clipper: BottomWaveClipper2(), | |
), | |
ClipPath( | |
child: new Container( | |
color: Colors.blue[300].withOpacity(0.5), | |
), | |
clipper: BottomWaveClipper3(), | |
), | |
], | |
); | |
} | |
} | |
class BottomWaveClipper extends CustomClipper<Path> { | |
@override | |
Path getClip(Size size) { | |
final Path path = Path(); | |
path.lineTo(0.0, size.height); | |
final Offset firstControlPoint = Offset(size.width / 4, size.height); | |
final Offset firstEndPoint = Offset(size.width / 2.25, size.height - 30.0); | |
path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy, | |
firstEndPoint.dx, firstEndPoint.dy); | |
final Offset secondControlPoint = | |
Offset(size.width - (size.width / 3.25), size.height - 65); | |
final Offset secondEndPoint = Offset(size.width, size.height - 10); | |
path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy, | |
secondEndPoint.dx, secondEndPoint.dy); | |
path.lineTo(size.width, size.height - 40); | |
path.lineTo(size.width, 0.0); | |
path.close(); | |
return path; | |
} | |
@override | |
bool shouldReclip(CustomClipper<Path> oldClipper) => false; | |
} | |
class BottomWaveClipper2 extends CustomClipper<Path> { | |
@override | |
Path getClip(Size size) { | |
final Path path = Path(); | |
path.lineTo(0.0, size.height - 15); | |
final Offset firstControlPoint = Offset(size.width / 4, size.height + 20); | |
final Offset firstEndPoint = Offset(size.width / 1.6, size.height - 30.0); | |
path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy, | |
firstEndPoint.dx, firstEndPoint.dy); | |
final Offset secondControlPoint = | |
Offset(size.width / 1.2, size.height - 65); | |
final Offset secondEndPoint = Offset(size.width, size.height - 20); | |
path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy, | |
secondEndPoint.dx, secondEndPoint.dy); | |
path.lineTo(size.width, size.height - 20); | |
path.lineTo(size.width, 0.0); | |
path.close(); | |
return path; | |
} | |
@override | |
bool shouldReclip(CustomClipper<Path> oldClipper) => false; | |
} | |
class BottomWaveClipper3 extends CustomClipper<Path> { | |
@override | |
Path getClip(Size size) { | |
final Path path = Path(); | |
path.lineTo(0.0, size.height - 35); | |
final Offset firstControlPoint = Offset(size.width / 4, size.height - 65); | |
final Offset firstEndPoint = Offset(size.width / 2, size.height - 10.0); | |
path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy, | |
firstEndPoint.dx, firstEndPoint.dy); | |
final Offset secondControlPoint = | |
Offset(size.width - (size.width / 3.25), size.height + 20); | |
final Offset secondEndPoint = Offset(size.width, size.height - 30); | |
path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy, | |
secondEndPoint.dx, secondEndPoint.dy); | |
path.lineTo(size.width, size.height - 20); | |
path.lineTo(size.width, 0.0); | |
path.close(); | |
return path; | |
} | |
@override | |
bool shouldReclip(CustomClipper<Path> oldClipper) => false; | |
} |
erlangparasu
commented
Jan 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment