当前位置:   article > 正文

flutter 绘制右上角圆角三角形标签_flutter container三角形

flutter container三角形

在这里插入图片描述
绘制:

import 'package:jade/utils/JadeColors.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class LabelTopRightYellow extends StatefulWidget {
  final String labelTitle; // 只能两个字的(文字偏移量没有根据文字长度改变)
  const LabelTopRightYellow({ this.labelTitle});
  
  State<StatefulWidget> createState() {
    return LabelViewState();
  }
}

class LabelViewState extends State<LabelTopRightYellow> with SingleTickerProviderStateMixin {

  
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: ArcPainter(labelTitle: widget.labelTitle),
      size: Size(35,35), // 调整大小以适应你的需求
    );
  }
}

class ArcPainter extends CustomPainter {

  //标签文字
  String labelTitle;
  ArcPainter({this.labelTitle});

  
  void paint(Canvas canvas, Size size) {
    double originX = 0.0 ;
    double originY = 0.0 ;

    double cx = size.width / 2;
    double cy = size.height / 2;
    Paint _paint = Paint()
      ..color = Color(0xffFFE50F)
      ..strokeWidth = 2
    //画笔是线段(默认是fill填充)
    /*..style = PaintingStyle.stroke*/;

    // canvas.drawCircle(Offset(cx,cy), 2, _paint);


    Path path = Path();
    // 绘制圆锥路径 权重越大路径越接近直角(不使用path.moveTo时,默认起点为左上角)
    path.conicTo(originX + size.width, originY, originX + size.width, originY+ size.height, 10);
    // 控制路径是否闭合,可不写
    path.close();
    canvas.drawPath(path, _paint);
    canvas.save();
    canvas.restore();

    TextPainter textPainterCenter = TextPainter(
      text: TextSpan(text: labelTitle, style: TextStyle(color: Color(0xff333333),fontSize: 10)),
      textDirection: TextDirection.ltr,
    );
    textPainterCenter.layout();
    canvas.rotate(math.pi / 4);
    canvas.translate(-math.pi , -((cy - math.pi)  * 2));
    textPainterCenter.paint(canvas, Offset(cx /*- textPainterCenter.size.width / 2*/,cy - textPainterCenter.size.height / 4));
    canvas.save();
    canvas.restore();
  }

  /// 度数转类似于π的那种角度
  double degToRad(double deg) => deg * (math.pi / 180.0);

  
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

引用:

Container(
	height: 180.w
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      padding: EdgeInsets.only(left: 20.w),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          boxShadow: [BoxShadow(color: JadeColors.grey_4,blurRadius: 2.0,offset: Offset(0.0, 0.0),)]
      ),
      child: Column(
        children: [
          _userView(),
          SizedBox(height: 20.w),
          _productView(),
          _countdownView()
        ],
      ),
    )
_userView(){
    return Stack(
      alignment: Alignment.topRight,
      children: [
        Row(
          children: [
            Container(
              width: 70.w,
              height: 70.w,
              margin: EdgeInsets.only(right: 20.w),
              child: Utils().roundedImage(PathConfig.testImageUrl[3], 70),
            ),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('商户商户商户商户',style: TextStyle(fontSize: 30.sp,color: JadeColors.grey_2,fontWeight: FontWeight.w600),maxLines: 1,overflow: TextOverflow.ellipsis,),
                Text('10分钟前',style: TextStyle(fontSize: 22.sp,color: JadeColors.grey))
              ],
            ))
          ],
        ),
        LabelTopRightYellow(labelTitle: S.current.cuxiao,)
      ],
    );
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/341758
推荐阅读
相关标签
  

闽ICP备14008679号