Created
November 12, 2024 19:50
-
-
Save jonahwilliams/0a3250e1fea11de8749253a19647b28b to your computer and use it in GitHub Desktop.
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'; | |
void main() { | |
runApp(const Example()); | |
} | |
class Example extends StatefulWidget { | |
const Example({super.key}); | |
@override | |
State<Example> createState() => _ExampleState(); | |
} | |
class _ExampleState extends State<Example> { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: ClipPath( | |
clipper: CurvedTopClipper(), | |
child: Image.network( | |
'https://picsum.photos/200/300' | |
)), | |
), | |
), | |
); | |
} | |
} | |
class CurvedTopClipper extends CustomClipper<Path> { | |
CurvedTopClipper({this.flip = false}); | |
final bool flip; | |
@override | |
Path getClip(Size size) { | |
double radius = size.width / 2; | |
var path = Path(); | |
if (flip) { | |
// path.addOval(Rect.fromCircle(center: Offset.zero, radius: 40)); | |
path.lineTo(0, size.height - radius); | |
path.arcToPoint( | |
Offset(size.width, size.height - radius), | |
radius: Radius.circular(size.width / 2), | |
clockwise: false, | |
); | |
path.lineTo(size.width, 0); | |
path.lineTo(0, 0); | |
} else { | |
path.lineTo(0, 0); | |
path.lineTo(0, radius); | |
path.arcToPoint( | |
Offset(size.width, radius), | |
radius: Radius.circular(radius / 2), | |
); | |
path.lineTo(size.width, size.height); | |
path.lineTo(0, size.height); | |
} | |
return path; | |
} | |
@override | |
bool shouldReclip(CurvedTopClipper oldClipper) => oldClipper.flip != flip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment