简体   繁体   English

如何在颤动中制作带有圆角边缘的按钮?

[英]How to make a button with rounded edges in flutter?

Making a rounded corner button is so simple, but I want to make a button that its edges are also rounded like this:制作圆角按钮很简单,但我想制作一个按钮,它的边缘也像这样:

在此处输入图片说明

maybe I should use CustomPaint ?也许我应该使用CustomPaint

Use a ContinuousRectangleBorder shape:使用ContinuousRectangleBorder形状:

ElevatedButton(
      style: ElevatedButton.styleFrom(
        shape: ContinuousRectangleBorder(
          side: BorderSide.none,
          borderRadius: BorderRadius.all(Radius.circular(18)),
        ),
      ),
      onPressed: () {},
      child: Text('Click'),
    );

That shape is called a Squircle.这种形状叫做松鼠。 See: https://en.wikipedia.org/wiki/Squircle请参阅: https : //en.wikipedia.org/wiki/Squircle

SquircleBorder might help you: SquircleBorder可能会帮助您:

Container(
            width: 56.0,
            height: 56.0,
            child: Material(
              color: Colors.blueGrey[400],
              shape: SquircleBorder(
                side: BorderSide(color: Colors.grey, width: 3.0),
              ),
              child: Icon(Icons.settings),
            ),
          ),

class SquircleBorder extends ShapeBorder {
  final BorderSide side;
  final double superRadius;

  const SquircleBorder({
    this.side: BorderSide.none,
    this.superRadius: 5.0,
  })
    : assert(side != null),
      assert(superRadius != null);

  @override
  EdgeInsetsGeometry get dimensions => EdgeInsets.all(side.width);

  @override
  ShapeBorder scale(double t) {
    return new SquircleBorder(
      side: side.scale(t),
      superRadius: superRadius * t,
    );
  }

  @override
  Path getInnerPath(Rect rect, {TextDirection textDirection}) {
    return _squirclePath(rect.deflate(side.width), superRadius);
  }

  @override
  Path getOuterPath(Rect rect, {TextDirection textDirection}) {
    return _squirclePath(rect, superRadius);
  }

  static Path _squirclePath(Rect rect, double superRadius) {
    final c = rect.center;
    final dx = c.dx * (1.0 / superRadius);
    final dy = c.dy * (1.0 / superRadius);
    return new Path()
      ..moveTo(c.dx, 0.0)
      ..relativeCubicTo(c.dx - dx, 0.0, c.dx, dy, c.dx, c.dy)
      ..relativeCubicTo(0.0, c.dy - dy, -dx, c.dy, -c.dx, c.dy)
      ..relativeCubicTo(-(c.dx - dx), 0.0, -c.dx, -dy, -c.dx, -c.dy)
      ..relativeCubicTo(0.0, -(c.dy - dy), dx, -c.dy, c.dx, -c.dy)
      ..close();
  }

  @override
  void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
    switch (side.style) {
      case BorderStyle.none:
        break;
      case BorderStyle.solid:
        var path = getOuterPath(rect.deflate(side.width / 2.0), textDirection: textDirection);
        canvas.drawPath(path, side.toPaint());
    }
  }
}

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

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