【问题标题】:How can I use two custom painters in Flutter?如何在颤振中使用两个自定义画家
【发布时间】:2020-07-31 14:20:24
【问题描述】:

我做了 2 个自定义画家

1.

class DrawTriangle1 extends CustomPainter {
  DrawTriangle1() {
    painter = Paint()
      ..shader =
          LinearGradient(colors: [Colors.red, Colors.white]).createShader(rect)
      ..style = PaintingStyle.fill;
  }

  @override
  void paint(Canvas canvas, Size size) {
    var path = Path();

    path.moveTo(0, 0);
    path.lineTo(size.width, 0);
    path.lineTo(size.width, size.height / 2);
    path.close();
    canvas.drawPath(path, painter);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
  1. class DrawTriangle2 extends CustomPainter {
      DrawTriangle2() {
        painter = Paint()
          ..shader = LinearGradient(colors: [
            Color(0xfffff),
            Color(0xff076585),
          ]).createShader(rect)
          ..style = PaintingStyle.fill;
      }
    
      @override
      void paint(Canvas canvas, Size size) {
        var path = Path();
        path.lineTo(size.width / 2, size.height / 4);
        path.lineTo(0, size.height / 2);
        path.close();
    
        canvas.drawPath(path, painter);
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
        return true;
      }
    }
    

如果我使用自定义画家制作单个形状。画家从页面开始绘制它。

ListView(
          children: <Widget>[
            CustomPaint(
              painter: DrawTriangle1(),
              size: Size(MediaQuery.of(context).size.width,
          MediaQuery.of(context).size.height), ,
            )
          ],
        )

但是如果我像这样在列表视图中添加另一个。第二个三角形从第一个屏幕的底部开始。

ListView(
          children: <Widget>[
            CustomPaint(
              painter: DrawTriangle1(),
              size: Size(MediaQuery.of(context).size.width,
                  MediaQuery.of(context).size.height),
            ),
            CustomPaint(
              painter: DrawTriangle2(),
              size: Size(MediaQuery.of(context).size.width,
                  MediaQuery.of(context).size.height),
            )
          ],
        ),

我如何让他们两个有相同的起点。

【问题讨论】:

    标签: flutter flutter-canvas


    【解决方案1】:

    使用堆栈。

    @override
      Widget build(BuildContext context) {
        return Stack(children: [
                CustomPaint(
                  painter: DrawTriangle1(),
                  size: Size(MediaQuery.of(context).size.width,
                      MediaQuery.of(context).size.height),
                ),
                CustomPaint(
                  painter: DrawTriangle2(),
                  size: Size(MediaQuery.of(context).size.width,
                      MediaQuery.of(context).size.height),
                )
              ]);
      }
    

    【讨论】:

      【解决方案2】:

      ListView 一个接一个地构建它的每个子级,因此如果您想在堆栈小部件方便的同一位置做一些工作,这是预期的行为。它将允许您在同一位置绘制两个自定义画家。

      所以用堆栈小部件替换你的 ListView 小部件将解决你的问题。

      Stack( //change 
                children: <Widget>[
                  CustomPaint(
                    painter: DrawTriangle1(),
                    size: Size(MediaQuery.of(context).size.width,
                        MediaQuery.of(context).size.height),
                  ),
                  CustomPaint(
                    painter: DrawTriangle2(),
                    size: Size(MediaQuery.of(context).size.width,
                        MediaQuery.of(context).size.height),
                  )
                ],
              ),
      

      【讨论】:

        猜你喜欢
        • 2022-12-22
        • 1970-01-01
        • 2020-10-13
        • 2020-07-27
        • 2020-11-01
        • 2019-02-07
        • 1970-01-01
        • 2021-04-05
        • 2021-01-27
        相关资源
        最近更新 更多