In flutter i want to clip optional part of Container
to make this widget:
clipping the widget to have two semicircle on top and bottom of that.
for some feature of this clipped widget i want to have some optional feature, for example:
clipping top, bottom of that optional and space from right. how can i clipping Container
with optional feature?
you can use custom clipper + arcToPoint path method to create clean arcs.
Like that:
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math.dart' as v_math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipPath(
clipper: DolDurmaClipper(right: 40, holeRadius: 20),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
color: Colors.blueAccent,
),
width: 300,
height: 95,
padding: EdgeInsets.all(15),
child: Text('first example'),
),
),
SizedBox(
height: 20,
),
ClipPath(
clipper: DolDurmaClipper(right: 100, holeRadius: 40),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
color: Colors.amber,
),
width: 200,
height: 250,
padding: EdgeInsets.all(35),
child: Text('second example'),
),
),
]);
}
}
class DolDurmaClipper extends CustomClipper<Path> {
DolDurmaClipper({@required this.right, @required this.holeRadius});
final double right;
final double holeRadius;
@override
Path getClip(Size size) {
final path = Path()
..moveTo(0, 0)
..lineTo(size.width - right - holeRadius, 0.0)
..arcToPoint(
Offset(size.width - right, 0),
clockwise: false,
radius: Radius.circular(1),
)
..lineTo(size.width, 0.0)
..lineTo(size.width, size.height)
..lineTo(size.width - right, size.height)
..arcToPoint(
Offset(size.width - right - holeRadius, size.height),
clockwise: false,
radius: Radius.circular(1),
);
path.lineTo(0.0, size.height);
path.close();
return path;
}
@override
bool shouldReclip(DolDurmaClipper oldClipper) => true;
}
To create same shape for left and right side of container you can use this.
class DolDurmaClipper extends CustomClipper<Path> {
final double holeRadius;
final double bottom;
DolDurmaClipper({required this.holeRadius, required this.bottom});
@override
Path getClip(Size size) {
final path = Path()
..moveTo(0, 0)
..lineTo(0.0, size.height - bottom - holeRadius)
..arcToPoint(
Offset(0, size.height - bottom),
clockwise: true,
radius: Radius.circular(1),
)
..lineTo(0.0, size.height)
..lineTo(size.width, size.height)
..lineTo(size.width, size.height - bottom)
..arcToPoint(
Offset(size.width, size.height - bottom - holeRadius),
clockwise: true,
radius: Radius.circular(1),
);
path.lineTo(size.width, 0.0);
path.close();
return path;
}
@override
bool shouldReclip(DolDurmaClipper oldClipper) => true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With