Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active November 21, 2024 12:18
Show Gist options
  • Save hectorAguero/ecb931b6112d26e35bb4f14fbec3d4fc to your computer and use it in GitHub Desktop.
Save hectorAguero/ecb931b6112d26e35bb4f14fbec3d4fc to your computer and use it in GitHub Desktop.
Flutter Custom ClipPath
import 'package:flutter/material.dart';
//https://www.youtube.com/watch?v=0PxX6LMnmwo&list=PLNF7sp688eT8gImxZlw4D0LhOwykuuskL
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Container(
color: Colors.blue,
child: ClipPath(
clipper: _PointedRectangleClipper(),
child: Container(
decoration: BoxDecoration(
color: Colors.red.withOpacity(0.9),
),
width: 500,
height: 1000,
),
),
),
),
),
);
}
}
class _PointedRectangleClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final path = Path();
final width = size.width;
final height = size.height;
const borderRadius = 20.0;
path
..moveTo(borderRadius, 0)
..lineTo(width - borderRadius, 0) // Top Right corner
..arcToPoint(
Offset(width, borderRadius),
radius: const Radius.circular(borderRadius),
)
..lineTo(width, height * 0.8) // Bottom Right corner
..moveConicTo(
Offset(width / 2, height),
Offset(0, height * 0.8),
weight: 10,
)
..lineTo(0, borderRadius) // Top Left corner
..arcToPoint(
const Offset(borderRadius, 0),
radius: const Radius.circular(borderRadius),
)
..close();
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
extension MyPath on Path {
void moveConicTo(Offset center, Offset end, {required double weight}) {
conicTo(
center.dx,
center.dy,
end.dx,
end.dy,
weight,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment