flutter - 在颤振中,CustomPaint() 在脚手架/容器小部件中使用时似乎表现得很奇怪
问题描述
所以,我试图在颤动中使用 CustomPaint 小部件在屏幕上绘制一条对角线。在 CustomPaint 小部件中,我正在调用名为 BottomCurvePainter 的 CustomPainter 类。代码是这样的。
class BottomCurvePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// TODO: implement paint
Paint paint = Paint();
paint.color = Colors.black;
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 5.0;
Path path = Path();
path.lineTo(size.width, size.height);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
//throw UnimplementedError();
return true;
}
}
我在家里这样称呼它的方式如下:-
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.orange,
child: CustomPaint(
painter: BottomCurvePainter(),
),
),
);
}
}
但这似乎不起作用。但是,如果我删除 Scaffold 小部件并直接返回 Container,我可以让它工作,就像这样 -
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.orange,
child: CustomPaint(
painter: BottomCurvePainter(),
),
);
}
}
有人可以解释为什么将容器包裹在脚手架中不起作用吗?谢谢。
解决方案
您应该指定容器的宽度和高度属性。它没有出现是因为您没有指定将在脚手架中发生的容器的尺寸(或约束)。从颤振文档中查看容器小部件。
推荐阅读
- javascript - 优化 React 应用程序 - 仅根据 React [React.JS]
- angular-cli - 如何在 Angular --prod build 中修复“TypeError: undefined is not a constructor (evalating 'new Lt.Subject')”?与 --optimization=false 一起使用
- flutter - 在颤动中动态获取上下文大小
- python - 具有多处理功能的 python 烧瓶生成作业
- date - 在 Tableau 中的给定日期获取该月的一周的开始日期
- jupyter-notebook - 执行 to_crs 以转换地理数据框的几何图形时内核死机
- php - 如何在 Lavarel 中使用 ID 获取属性名称
- android-studio - 使用错误的密钥签名的 Android App Bundle
- reactjs - 输入'{标签:字符串;值:字符串;}' 在 Typescript react js 中缺少以下类型的属性
- sql - postgresql afew 是什么意思?