Flutter 如何在行小部件中定位CustomPaint?

Flutter 如何在行小部件中定位CustomPaint?,flutter,custom-painting,Flutter,Custom Painting,我是新来的。 我想将progressbar(用custompainter制作)放置在一行中,但我不知道如何操作。我希望行中的所有元素对齐,垂直居中,并且它们之间的空间相同 我的代码是: Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Container(alignment: Alignment.center, child: Row( mainAxisAlignmen

我是新来的。 我想将progressbar(用custompainter制作)放置在一行中,但我不知道如何操作。我希望行中的所有元素对齐,垂直居中,并且它们之间的空间相同

我的代码是:

Widget build(BuildContext context) {
return MaterialApp(
    home: Scaffold(
  body: Container(alignment: Alignment.center, child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: <Widget>[
      Text('text1'),
      Text('text2'),
      CustomPaint(painter: progressBar(),),
      Text('text3'),
      Text('text4')
    ],
  ),) 
));

在当前代码中,CustommPaint的大小为0。CustomPaint的属性提到了它是如何获得其大小的,因此您需要为子级提供大小或提供其大小属性(如size:size(50,14))。此外,在“绘制方法”中,还应使用“大小”参数,以便仅在可用空间中绘制,而不会超出边界(与当前一样):


只需添加大小:
CustomPaint(画师:progressBar(),大小:大小(宽度、高度),,
class progressBar extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();


Radius corner =Radius.circular(8);

paint.color =Color.fromRGBO(0, 0, 0, 1);
canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(0, 0, 250, 14), corner), paint);

paint.color =Color.fromRGBO(191, 20, 28, 1);
canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(1, 2, 50, 10), corner), paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(0, 0, size.width, size.height), corner), paint);