我想在Flutter中为小部件创建一个超椭圆形。
我找到了一篇有关创建用python和java编写的超级省略号的文章,但是我无法完全使代码正常工作。
class SuperEllipse extends ShapeBorder {
final BorderSide side;
final double n;
SuperEllipse({
@required this.n,
this.side = BorderSide.none,
}) : assert(side != null);
@override
EdgeInsetsGeometry get dimensions => EdgeInsets.all(side.width);
@override
ShapeBorder scale(double t) {
return SuperEllipse(
side: side.scale(t),
n: n,
);
}
@override
Path getInnerPath(Rect rect, {TextDirection textDirection}) {
return _superEllipsePath(rect, n);
}
@override
Path getOuterPath(Rect rect, {TextDirection textDirection}) {
return _superEllipsePath(rect, n);
}
static Path _superEllipsePath(Rect rect, double n) {
final int a = 200;
List<double> points = [a + 1.0];
Path path = new Path();
path.moveTo(a.toDouble(), 0);
// Calculate first quadrant.
for (int x = a; x >= 0; x--) {
points[x] = pow(pow(a, n) - pow(x, n), 1 / n);
path.lineTo(x.toDouble(), -points[x]);
}
// Mirror to other quadrants.
for (int x = 0; x <= a; x++) {
path.lineTo(x.toDouble(), points[x]);
}
for (int x = a; x >= 0; x--) {
path.lineTo(-x.toDouble(), points[x]);
}
for (int x = 0; x <= a; x++) {
path.lineTo(-x.toDouble(), -points[x]);
}
return path;
}
@override
void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
Path path = getOuterPath(rect.deflate(side.width / 2.0), textDirection: textDirection);
canvas.drawPath(path, side.toPaint());
}
}
我想返回正确的形状,但是却出现异常:无效值:仅有效值为0:200。
由于某种原因,变量a
不允许为200吗?我不知道为什么,将其更改为0不会产生任何错误,但是也没有形状。
有人知道这样做是否有更好的方法吗?
答案 0 :(得分:0)