简体   繁体   English

如何制作特定的矩形 flutter

[英]How to make a specific rectangle flutter

I want to make shape like the one below and write on it in my flutter app我想制作像下面这样的形状并在我的 flutter 应用程序中写上它在此处输入图像描述

How can I make it?我怎样才能做到?

you can use ClipPath你可以使用 ClipPath

 class _BottomBarState extends State<BottomBar> {
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: ClipPath(
        clipper: MyCustomClipper(),
        child: Container(
          width: 300,
          height: 50,
          color: Colors.red,
          alignment: Alignment.center,
          child:const Text("Your Text"),
        ),
      ),
    ));
  }
}

class MyCustomClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();

    path.lineTo(size.width, 0);
    path.lineTo(size.width - 50, 25);
    path.lineTo(size.width, size.height);
    path.lineTo(0, size.height);

    return path;
  }

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

输出

You should make a CustomPainter like this:您应该像这样制作CustomPainter

class MyCustomRectangle extends CustomPainter {
  final Color color;

  const MyCustomRectangle({
    this.color = Colors.red,
  });

  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint();
    paint.color = color;
    paint.style = PaintingStyle.fill;

    var path = Path();

    path.lineTo(size.width, 0);
    path.lineTo(size.width - (size.height / 2), size.height / 2);
    path.lineTo(size.width, size.height);
    path.lineTo(0, size.height);

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => oldDelegate != this;
}

then use it like this:然后像这样使用它:

CustomPaint(
          painter: MyCustomRectangle(),
          child: Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 36.0,
              vertical: 8.0,
            ),
            child: Text(
              "Free Home Delivery 30 min",
              style: TextStyle(
                color: Colors.white,
                fontSize: 23.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        )

the output: output:
在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM