简体   繁体   中英

Flutter create container in shape from sketch

How to create container like this, bottom border must be curved在此处输入图片说明

You can make a CustomPainter to draw the shape:

class MyPainter extends CustomPainter {
  final Color color;
  final double strokeWidth;

  MyPainter({this.color = Colors.black, this.strokeWidth = 5.0});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth;
    final path = Path()
      ..lineTo(size.width, 0)
      ..lineTo(size.width, size.height)
      ..moveTo(0, 0)
      ..lineTo(0, size.height)
      ..moveTo(0, size.height)
      ..quadraticBezierTo(size.width / 2, 0, size.width, size.height);
    canvas.drawPath(path, paint);
  }

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

Usage:

CustomPaint(
  painter: MyPainter(strokeWidth: 4),
  child: Container(
    width: 300,
    height: 50,
  ),
)

Result:

资源


Note: Instead of quadricBezierTo you can use arcTo :

(remember to import dart:math to use pi )

...
    final path = Path()
      ..lineTo(size.width, 0)
      ..lineTo(size.width, size.height)
      ..moveTo(0, 0)
      ..lineTo(0, size.height)
      ..moveTo(0, size.height)
      ..arcTo(Rect.fromLTWH(0, size.height / 2, size.width, size.height), -pi,
          pi, false); // arc instead of quadricBezier
...

Result:

资源2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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