Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created November 12, 2024 19:50
Show Gist options
  • Save jonahwilliams/0a3250e1fea11de8749253a19647b28b to your computer and use it in GitHub Desktop.
Save jonahwilliams/0a3250e1fea11de8749253a19647b28b to your computer and use it in GitHub Desktop.
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