Android 如何使用ClipPath颤振
我想为Android 如何使用ClipPath颤振,android,flutter,flutter-layout,Android,Flutter,Flutter Layout,我想为容器提供上述背景,尝试了以下代码。但它不起作用 ClipPath( clipper: TriangleClipper(), child: Container( margin: EdgeInsets.only(top: 20, right: 20), padding: EdgeInsets.all(20), decoration: BoxDecora
容器
提供上述背景,尝试了以下代码。但它不起作用
ClipPath(
clipper: TriangleClipper(),
child: Container(
margin: EdgeInsets.only(top: 20, right: 20),
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: cardBackground,
borderRadius: BorderRadius.circular(10))),
Class TriangleClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final path = Path();
path.lineTo(0.0, size.height);
path.lineTo(size.width, size.height);
path.close();
return path;
}
@override
bool shouldReclip(TriangleClipper oldClipper) => false;
}
ClipPath(
裁剪器:三角形裁剪器(),
子:容器(
页边距:仅限边集(顶部:20,右侧:20),
填充:边缘设置。全部(20),
装饰:盒子装饰(
颜色:卡片背景,
边界半径:边界半径。圆形(10)),
类TriangleClipper扩展了CustomClipper{
@凌驾
路径getClip(大小){
最终路径=路径();
path.lineTo(0.0,大小.高度);
path.lineTo(大小.宽度,大小.高度);
path.close();
返回路径;
}
@凌驾
bool shouldReclip(triangeleclipper oldcipper)=>false;
}
出于这种目的,最好使用自定义形状
使用
ClipPath
,您需要绘制要雕刻的整个路径,其中包括绘制三角形以及左上角和右上角圆角,因为相应的原始角(borderRadius:borderRadius.circular(10)
)将被裁剪掉。你可以这样做
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return ClipPath(
clipper: TriangleClipper(),
child: Container(
padding: EdgeInsets.fromLTRB(20, 30, 20, 20), //extra 10 for top padding because triangle's height = 10
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(10),
),
child: Text('My parent is a fancy container'),
),
);
}
}
class TriangleClipper extends CustomClipper<Path> {
double radius = 10, tw = 20, th = 10; //tw & th = triangle width & height
@override
Path getClip(Size size) {
final path = Path();
path.lineTo(0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, th + radius);
path.arcToPoint(Offset(size.width - radius, th), radius: Radius.circular(radius), clockwise: false);
path.lineTo(radius + 10 + tw, th);
path.lineTo(radius + 10 + tw/2, 0); //in these lines, the 10 is to have a space of 10 between the top-left corner curve and the triangle
path.lineTo(radius + 10, th);
path.lineTo(radius, th);
path.arcToPoint(Offset(0, th + radius), radius: Radius.circular(radius), clockwise: false);
return path;
}
@override
bool shouldReclip(TriangleClipper oldClipper) => false;
}
class\u MyWidgetState扩展状态{
@凌驾
小部件构建(构建上下文){
返回ClipPath(
裁剪器:三角形裁剪器(),
子:容器(
padding:EdgeInsets.fromLTRB(20,30,20,20),//由于三角形的高度=10,顶部填充额外增加10
装饰:盒子装饰(
颜色:颜色。绿色,
边界半径:边界半径。圆形(10),
),
child:Text(“我的父母是一个奇特的容器”),
),
);
}
}
类TriangleClipper扩展了CustomClipper{
双半径=10,tw=20,th=10;//tw&th=三角形宽度和高度
@凌驾
路径getClip(大小){
最终路径=路径();
path.lineTo(0,大小.高度);
path.lineTo(大小.宽度,大小.高度);
路径.lineTo(大小.宽度,th+半径);
路径.arcToPoint(偏移量(size.width-radius,th),半径:radius.circular(radius),顺时针:false);
线路走向(半径+10+tw,th);
path.lineTo(radius+10+tw/2,0);//在这些直线中,10在左上角曲线和三角形之间的间距为10
线路走向(半径+10,th);
线路到(半径,th);
弧点(偏移量(0,th+半径),半径:半径。圆形(半径),顺时针:假);
返回路径;
}
@凌驾
bool shouldReclip(triangeleclipper oldcipper)=>false;
}
结果:
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return ClipPath(
clipper: TriangleClipper(),
child: Container(
padding: EdgeInsets.fromLTRB(20, 30, 20, 20), //extra 10 for top padding because triangle's height = 10
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(10),
),
child: Text('My parent is a fancy container'),
),
);
}
}
class TriangleClipper extends CustomClipper<Path> {
double radius = 10, tw = 20, th = 10; //tw & th = triangle width & height
@override
Path getClip(Size size) {
final path = Path();
path.lineTo(0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, th + radius);
path.arcToPoint(Offset(size.width - radius, th), radius: Radius.circular(radius), clockwise: false);
path.lineTo(radius + 10 + tw, th);
path.lineTo(radius + 10 + tw/2, 0); //in these lines, the 10 is to have a space of 10 between the top-left corner curve and the triangle
path.lineTo(radius + 10, th);
path.lineTo(radius, th);
path.arcToPoint(Offset(0, th + radius), radius: Radius.circular(radius), clockwise: false);
return path;
}
@override
bool shouldReclip(TriangleClipper oldClipper) => false;
}