【问题标题】:Custom Painter class not visible in Stack flutter自定义 Painter 类在 Stack Flutter 中不可见
【发布时间】:2020-05-18 05:47:31
【问题描述】:

由于某种原因,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;
  }

谢谢!

【问题讨论】:

    标签: flutter flutter-layout flutter-canvas


    【解决方案1】:

    检查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(),
          ),
        )
      ],
    );
    

    最终结果是

    【讨论】:

    • 那么从堆栈中删除它时如何在没有大小的情况下工作?
    • 如果你给堆栈内的容器指定宽度和高度,那么这些宽度和高度将被 custompaint 使用。
    • 有什么问题?
    • No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you do not have a WidgetsApp or MaterialApp widget
    • 是的,我知道原因;它已经在代码中修复了。树中需要有一个更高的 MaterialApp。我建议您完全按照回答的方式粘贴代码,检查结果是否适合您。注释其余代码并在答案中使用代码。
    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 2012-04-18
    • 2014-03-18
    • 1970-01-01
    相关资源
    最近更新 更多