gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:42:55 30 4
gpt4 key购买 nike

这是自定义形状代码

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中定义子项
我添加了一个空容器并运行代码,并得到了此结果
Screenshot
也建议不要命名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;
}
}

关于flutter - 我正在尝试在 flutter 中绘制自定义形状,但是不幸的是该形状没有出现,我只看到了白色的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62841693/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com