我通过为AppBar 边框提供自定义形状来创建所需的AppBar 形状,查看实时示例here。
如果您想剪裁AppBar,您也可以在剪裁器中使用类似的Path,但我认为为边框提供自定义形状会更好。
自定义AppBar边框形状的代码:
class CustomAppBarShape extends ContinuousRectangleBorder {
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
double height = rect.height;
double width = rect.width;
var path = Path();
path.lineTo(0, height + width * 0.1);
path.arcToPoint(Offset(width * 0.1, height),
radius: Radius.circular(width * 0.1),
);
path.lineTo(width * 0.9, height);
path.arcToPoint(Offset(width, height + width * 0.1),
radius: Radius.circular(width * 0.1),
);
path.lineTo(width, 0);
path.close();
return path;
}
}
编辑:
对于动态边缘半径,使用乘数 multi 作为构造函数参数更新 customAppBarShape,并使用 multi 创建外部路径。
class CustomAppBarShape extends ContinuousRectangleBorder {
final double multi;
const CustomAppBarShape({this.multi = 0.1});
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
double height = rect.height;
double width = rect.width;
var path = Path();
path.lineTo(0, height + width * multi);
path.arcToPoint(Offset(width * multi, height),
radius: Radius.circular(width * multi),
);
path.lineTo(width * (1 - multi), height);
path.arcToPoint(Offset(width, height + width * multi),
radius: Radius.circular(width * multi),
);
path.lineTo(width, 0);
path.close();
return path;
}
}
然后像这样在构造函数参数中发送你想要的值,
appBar: AppBar(
title: Text(widget.title),
shape: const CustomAppBarShape(multi: 0.02),
)
确保 multi 的值小于 1 以获得所需的形状。