我正在尝试在颤振中绘制自定义形状,但不幸的是形状没有出现,我只看到白色容器

  • 本文关键字:白色 自定义 绘制 flutter dart
  • 更新时间 :
  • 英文 :


这是自定义形状代码

class SignInPageCustomShape extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint();
final Color myColor = Colors.black12;
paint.color = myColor;
paint.style = PaintingStyle.fill;
final path = Path();
path.moveTo(0, size.height * 0.9167);
path.quadraticBezierTo(
size.width * 0.25,
size.height * 0.875,
size.width * 0.5,
size.height * 0.9167,
);
path.quadraticBezierTo(
size.width * 0.75,
size.height * 0.9584,
size.width * 1.0,
size.height * 0.9167,
);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

这是我的小部件树,我不知道出了什么问题,我试图把我的容器放在不同的地方,但得到了相同的结果,堆栈溢出要求我解释更多,但我没有其他东西要解释。

class Paint extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: CustomPaint(
painter: SignInPageCustomShape(),
),
),
),
);
}
}

您面临的问题可能是因为您尚未在CustomPaint 中定义子项

我添加了一个空容器并运行代码,得到了以下结果

屏幕截图

不建议将您的StatelessWidget命名为Paint

class Paint extends StatelessWidget {

下面的方法接受两个参数,一个是Paint类型,因为您定义了一个自定义小部件,该小部件的名称与我的中传递的参数相同

canvas.drawPath(path, paint);

我用来获取此响应中所附结果的代码:绘画小工具:

class PaintWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: CustomPaint(
painter: SignInPageCustomShape(),
child: Container(),
),
),
),
);
}
}

SignInPageCustomShape小工具:

class SignInPageCustomShape extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint();
final Color myColor = Colors.black12;
paint.color = myColor;
paint.style = PaintingStyle.fill;
final path = Path();
path.moveTo(0, size.height * 0.9167);
path.quadraticBezierTo(
size.width * 0.25,
size.height * 0.875,
size.width * 0.5,
size.height * 0.9167,
);
path.quadraticBezierTo(
size.width * 0.75,
size.height * 0.9584,
size.width * 1.0,
size.height * 0.9167,
);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

最新更新