[英]flutter: CustomPaint widget turned blank when put in a Column
I've got something super bizarre:我有一些超级奇怪的东西:
The following code paints a rectangle with CustomPaint
.以下代码使用
CustomPaint
绘制一个矩形。 This version works fine这个版本工作正常
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_ MyAppState createState() => _ MyAppState();
}
class _MyAppState extends State<MyApp> {
EmbeddedPainter _painter;
@override
void initState() {
super.initState();
_painter = EmbeddedPainter();
}
@override
Widget build(BuildContext context) {
return Container(
child: ClipRect(
child: CustomPaint(
painter: _painter
),
),
);
}
}
Then as soon as I place this CustomPaint
into a Column
widget, I no longer see the painted rectangle.然后,一旦我将此
CustomPaint
放入Column
小部件中,我就不再看到绘制的矩形。
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_ MyAppState createState() => _ MyAppState();
}
class _MyAppState extends State<MyApp> {
EmbeddedPainter _painter;
@override
void initState() {
super.initState();
_painter = EmbeddedPainter();
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
ClipRect(
child: CustomPaint(
painter: _painter
),
),
],
)
);
}
}
The painter looks like this画师长这样
class EmbeddedPainter extends CustomPainter with ChangeNotifier {
var _paint = Paint()
..strokeJoin = StrokeJoin.miter
..strokeWidth = 1.0
..color = Colors.green
..style = PaintingStyle.fill;
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(Rect.fromLTWH(50, 50, 100, 100), _paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
void update(Color color0, Color color1) {
// draw
notifyListeners();
}
}
Solved it myself:自己解决了:
The ClipRect
must be wrapped with a container with size, eg, a SizedBox
. ClipRect
必须用具有大小的容器包裹,例如SizedBox
。
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
SizedBox(
width: 500,
height: 300,
child: ClipRect(
child: CustomPaint(
painter: _painter
),
),
)
],
)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.