use*_*882 1 flutter flutter-layout flutter-canvas
出于某种原因,Stack小部件不会显示Container带有CustomPaint.
但是,如果从 中删除Stack,它可以正常工作。我在这里缺少什么?
class _DemoNavBar extends State<DemoNavBar> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home:
Stack(
children: <Widget>[
Container(child: CustomPaint(painter: CurvePainter()))
],
)
);
}
}
class CurvePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.green[800];
paint.style = PaintingStyle.fill;
var path = Path();
path.moveTo(0, size.height - 100);
path.lineTo(size.width * 0.5, size.height - 100);
path.quadraticBezierTo(size.width * 0.7, size.height, size.width * 0.9,
size.height - 100);
path.lineTo(size.width, size.height - 100);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
检查CustomPaint它的来源说
/// The size that this [CustomPaint] should aim for, given the layout
/// constraints, if there is no child.
///
/// Defaults to [Size.zero].
///
/// If there's a child, this is ignored, and the size of the child is used
/// instead.
Run Code Online (Sandbox Code Playgroud)
所以,给它一个大小。其他解决方案包括 1) 为 的父级提供宽度和高度Container,CustomPaint以及 2) 为其提供一个子级,CustomPaint这将忽略size以下解决方案中提供的。
我检查了这段代码工作正常。
size: MediaQuery.of(context).size使用完整的屏幕尺寸。
void main() {
runApp(SO());
}
class SO extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DemoNavBar(),
);
}
}
class DemoNavBar extends StatefulWidget {
@override
_DemoNavBar createState() => _DemoNavBar();
}
class _DemoNavBar extends State<DemoNavBar> {
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
CustomPaint(
size: MediaQuery.of(context).size,
painter: CurvePainter(),
)
],
);
}
}
class CurvePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.green[800];
paint.style = PaintingStyle.fill;
var path = Path();
path.moveTo(0, size.height - 100);
path.lineTo(size.width * 0.5, size.height - 100);
path.quadraticBezierTo(size.width * 0.7, size.height, size.width * 0.9, size.height - 100);
path.lineTo(size.width, size.height - 100);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,原因是因为Container没有父级或子级来提供它需要完整屏幕大小的大小,并且没有Stack. 当使用堆栈时,大小变为零,这将提供给自定义画家。
等价的代码可以写成
Stack(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: CustomPaint(
painter: CurvePainter(),
),
)
],
);
Run Code Online (Sandbox Code Playgroud)
最终结果是
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |