gpt4 book ai didi

flutter - 自定义 Painter 类在 Stack Flutter 中不可见

转载 作者:行者123 更新时间:2023-12-05 00:46:31 26 4
gpt4 key购买 nike

由于某种原因,Stack 小部件没有显示带有 CustomPaintContainer

但是,如果从 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;
}

谢谢!

最佳答案

检查 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.

所以,给它一个大小。其他解决方案包括 1) 为 CustomPaint 的父 Container 提供宽度和高度,以及 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;
}
}

现在,原因是因为 Container 没有父或子来提供它需要完整屏幕大小的大小,并且在没有 Stack 的情况下也可以正常工作。当使用堆栈时,大小会变为零,这会提供给自定义画家。

等价的代码可以写成

Stack(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: CustomPaint(
painter: CurvePainter(),
),
)
],
);

最终结果是

screenshot

关于flutter - 自定义 Painter 类在 Stack Flutter 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022832/

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