flutter - 如何在不使用边界半径的情况下创建弯曲的小部件
解决方案
这是一个示例,您如何使用它来实现它CustomClipper
class ClippingApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ClipPath(
clipper: CurvedBottomClipper(),
child: Container(
color: Colors.lightGreen,
height: 250.0,
child: Center(
child: Padding(
padding: EdgeInsets.only(bottom: 50),
child: Text(
"Curved View",
style: TextStyle(
fontSize: 25,
color: Colors.white,
),
),
)),
),
),
);
}
}
class CurvedBottomClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
// I've taken approximate height of curved part of view
// Change it if you have exact spec for it
final roundingHeight = size.height * 3 / 5;
// this is top part of path, rectangle without any rounding
final filledRectangle =
Rect.fromLTRB(0, 0, size.width, size.height - roundingHeight);
// this is rectangle that will be used to draw arc
// arc is drawn from center of this rectangle, so it's height has to be twice roundingHeight
// also I made it to go 5 units out of screen on left and right, so curve will have some incline there
final roundingRectangle = Rect.fromLTRB(
-5, size.height - roundingHeight * 2, size.width + 5, size.height);
final path = Path();
path.addRect(filledRectangle);
// so as I wrote before: arc is drawn from center of roundingRectangle
// 2nd and 3rd arguments are angles from center to arc start and end points
// 4th argument is set to true to move path to rectangle center, so we don't have to move it manually
path.arcTo(roundingRectangle, pi, -pi, true);
path.close();
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
// returning fixed 'true' value here for simplicity, it's not the part of actual question, please read docs if you want to dig into it
// basically that means that clipping will be redrawn on any changes
return true;
}
}
推荐阅读
- linux - 用于 grep 选择日期范围的 unix 脚本?
- sql - 从两个表更新新创建的表 - SQL
- jmeter - JMeter - 如何批处理线程组中的线程
- objective-c - 片段着色器功能中 float4 和 half4 的主要区别是什么?
- c++ - 为什么pair在初始化中不需要类型
- php - 使用 LIKE 子句和 NO_BACKSLASH_ESCAPES 模式在 mysqli prepare() 中转义 % 符号
- docker - 微服务:docker.compose 文件中的 ESHOP_OCELOT_VOLUME_SPEC 行是什么意思
- javascript - Handsontable 中的 Handsontable 下拉高度调整
- quartz-scheduler - 泽西集成石英 @Inject 注释不起作用
- excel - 如何从 Sheet2 和 Sheet3 索引以查找 Sheet1 的值