import 'package:flutter/material.dart';
class ClipDemoScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Clip Demo Screen')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg',
width: 150,
height: 150,
fit: BoxFit.cover,
),
),
SizedBox(height: 40),
ClipPath(
clipper: TriangleClipper(),
child: Container(
width: 150,
height: 150,
color: Colors.blueAccent,
),
),
],
),
),
);
}
}
class TriangleClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final path = Path();
path.moveTo(size.width / 2, 0); // Top center
path.lineTo(0, size.height); // Bottom left
path.lineTo(size.width, size.height); // Bottom right
path.close();
return path;
}
@override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => false;
}We use ClipRRect to clip the image with rounded corners by setting borderRadius to 20 pixels. This makes the image corners smooth and rounded.
Below it, we use ClipPath with a custom clipper TriangleClipper that clips a container into a triangle shape. The getClip method defines the triangle path by connecting the top center point to bottom left and bottom right corners.
Both clipped widgets are centered vertically with spacing using SizedBox.