【问题标题】:flutter: custom shape as text background颤振:自定义形状作为文本背景
【发布时间】:2021-03-22 12:15:54
【问题描述】:

我需要在颤振中实现这一点:

我尝试制作Row 并为Text 设置背景来处理直角,然后使用ClipPath 来制作三角形。这样做的问题是边缘不精确(您可以在下图中看到矩形和三角形之间的细线),ClipPath 也有固定大小,所以如果我想在某个时候更改文本大小我将不得不再次微调三角形。

这是我的代码:

class TriangleClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();
    path.lineTo(0.0, size.height);
    path.lineTo(size.width / 3, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(TriangleClipper oldClipper) => false;
}

Row(
        children: [
          Container(
            color: Colors.orange,
            child: Padding(
              padding: const EdgeInsets.all(4.0),
              child: Text(
                "a label",
              ),
            ),
          ),
          ClipPath(
            clipper: TriangleClipper(),
            child: Container(
              color: Colors.orange,
              height: 23,
              width: 20,
            ),
          )
        ],
      )

我虽然在ClipPath 的孩子中使用double.infinity 而不是固定大小可以解决这个问题,但这样做会使三角形消失(可能它变得太大以至于我看不到它或落后于某些东西)。

解决这个问题的正确方法是什么?我想我可以使用ClipPath 绘制梯形,但是如何使它的宽度适应Text 的长度?

【问题讨论】:

    标签: flutter shapes


    【解决方案1】:

    实现这一点的一种方法是这样的。

    class TriangleClipper extends CustomClipper<Path> {
      @override
      Path getClip(Size size) {
        final w = size.width;
        final h = size.height;
        final triangleWidth = 10;
    
        final path = Path();
        path.lineTo(0, 0);
        path.lineTo(0, h);
        path.lineTo(w, h);
        path.lineTo(w - triangleWidth, 0);
        path.close();
        return path;
      }
    
      @override
      bool shouldReclip(TriangleClipper oldClipper) => false;
    }
    

    并像这样使用它。

    ClipPath(
        clipper: TriangleClipper(),
        child: Container(
          padding: EdgeInsets.fromLTRB(4, 4, 14, 4),   // 4 + triangleWidth for right padding
          color: Colors.orange,
          child: Text('A label'),
        ),
    ),
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码:

      import 'dart:ui' as ui;
      
      child: CustomPaint(
        size: Size(WIDTH,(WIDTH*0.625).toDouble()), //You can Replace [WIDTH] with your desired width for Custom Paint and height will be calculated automatically
        painter: RPSCustomPainter(),
      ),
      
      
      class RPSCustomPainter extends CustomPainter{
        
        @override
        void paint(Canvas canvas, Size size) {
          
          
      
        Paint paint_0 = new Paint()
            ..color = Color.fromARGB(255, 33, 150, 243)
            ..style = PaintingStyle.fill
            ..strokeWidth = 1;
          paint_0.shader = ui.Gradient.linear(Offset(size.width*0.29,size.height*0.28),Offset(size.width*0.29,size.height*0.28),[Color(0xff7c1010),Color(0xffffffff)],[0.00,1.00]); 
               
          Path path_0 = Path();
          path_0.moveTo(size.width*0.2937500,size.height*0.2780000);
      
          canvas.drawPath(path_0, paint_0);
        
      
        Paint paint_1 = new Paint()
            ..color = Color.fromARGB(255, 33, 150, 243)
            ..style = PaintingStyle.fill
            ..strokeWidth = 1;
           
               
          Path path_1 = Path();
          path_1.moveTo(size.width*0.3750000,size.height*0.4000000);
          path_1.lineTo(size.width*0.5000000,size.height*0.4000000);
          path_1.lineTo(size.width*0.5375000,size.height*0.5000000);
          path_1.lineTo(size.width*0.3750000,size.height*0.5000000);
          path_1.lineTo(size.width*0.3750000,size.height*0.4000000);
          path_1.close();
      
          canvas.drawPath(path_1, paint_1);
        
          
        }
      
        @override
        bool shouldRepaint(covariant CustomPainter oldDelegate) {
          return true;
        }
        
      }
      

      你可以自己试试:https://shapemaker.web.app/#/

      【讨论】:

      • 那不是使用固定宽度吗?我需要根据标签长度调整形状大小
      猜你喜欢
      • 2022-07-27
      • 2021-09-16
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2021-04-05
      相关资源
      最近更新 更多