颤动的圆角矩形边框,每侧具有不同的颜色

时间:2019-07-08 05:26:36

标签: flutter dart

我正在尝试重新创建一个由React Native制作的按钮,该按钮的每一面都有不同的颜色,使其具有一种类似于凿子效果的效果,有点像Photoshop的斜角和浮雕,但是还带有圆角。目前,我在按钮外部有一个带有边框的容器,而在容器内部我正在使用 MyJsonObjectRequest jsonObjReq = new MyJsonObjectRequest( false, getActivity(), Request.Method.POST, url, requestObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { if (response == null) { return; } Utils.DEBUG(TAG, "RequestBody : " + new Gson().toJson(requestObject)); Utils.DEBUG(TAG, "onResponse() called : " + new Gson().toJson(response)); String responseString = new Gson().toJson(response); fPOMobileReviewPassData = ParseManager.getInstance().fromJSON(response, FPOMobileReviewPassData.class); airlineId = fPOMobileReviewPassData.getAirlineId(); updateUI(); if (flagAddUserAPI) { callAddUserApi(loginData); } Utils.hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Utils.showVolleyError(error, getActivity()); Utils.hideProgressDialog(); } } ); // Adding request to request queue AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj); 。容器的代码如下:

RawMaterialButton

如何使此边框/容器的角变圆?

像这样,但带有圆角:

enter image description here

2 个答案:

答案 0 :(得分:2)

这不是100%有效的解决方案。我刚刚编写了一些Path函数,以使用 def process(arg): try: return eval(arg) except: return arg 绘制所有面。

圆角轮廓边框

CustomPainter

RoundedBorderPainterBorder

class RoundedChiseledBorder extends StatelessWidget {
  final Widget child;

  final Color leftBorderColor;
  final Color rightBorderColor;
  final Color bottomBorderColor;
  final Color topBorderColor;

  final double borderRadius;
  final double borderWidth;

  RoundedChiseledBorder({
    @required this.child,
    this.borderRadius = 1,
    this.borderWidth = 2,
    this.bottomBorderColor = Colors.black,
    this.topBorderColor = Colors.black,
    this.rightBorderColor = Colors.black,
    this.leftBorderColor = Colors.black,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Positioned.fill(
          child: CustomPaint(
            painter: RoundedBorderPainter(
              radius: borderRadius,
              strokeWidth: borderWidth,
              bottomBorderColor: bottomBorderColor,
              leftBorderColor: leftBorderColor,
              rightBorderColor: rightBorderColor,
              topBorderColor: topBorderColor,
            ),
          ),
        ),
        child,
      ],
    );
  }
}

用法

要使用它,只需致电

import 'package:vector_math/vector_math.dart' as vm;

class RoundedBorderPainter extends CustomPainter {
  final Color leftBorderColor;
  final Color rightBorderColor;
  final Color bottomBorderColor;
  final Color topBorderColor;
  final double strokeWidth;
  final StrokeCap strokeCap = StrokeCap.round;
  double radius;

  Size size;

  RoundedBorderPainter({
    this.leftBorderColor = Colors.black,
    this.rightBorderColor = Colors.black,
    this.topBorderColor = Colors.black,
    this.bottomBorderColor = Colors.black,
    this.strokeWidth = 2,
    this.radius = 1,
  }) {
    if (radius <= 1) this.radius = 1;
  }

  @override
  void paint(Canvas canvas, Size size) {
    radius = size.shortestSide / 2 < radius ? size.shortestSide / 2 : radius;
    this.size = size;
    Paint topPaint = Paint()
      ..color = topBorderColor
      ..strokeWidth = strokeWidth
      ..strokeCap = strokeCap
      ..style = PaintingStyle.stroke;
    Paint rightPaint = Paint()
      ..color = rightBorderColor
      ..strokeCap = strokeCap
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;
    Paint bottomPaint = Paint()
      ..color = bottomBorderColor
      ..strokeCap = strokeCap
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;
    Paint leftPaint = Paint()
      ..strokeCap = strokeCap
      ..color = leftBorderColor
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    canvas.drawPath(getPath1(), topPaint);
    canvas.drawPath(getPath2(), rightPaint);
    canvas.drawPath(getPath3(), bottomPaint);
    canvas.drawPath(getPath4(), leftPaint);
  }

  Path getPath1() {
    return Path()
      ..addPath(getTopLeftPath2(), Offset(0, 0))
      ..addPath(getTopPath(), Offset(0, 0))
      ..addPath(getTopRightPath1(), Offset(0, 0));
  }

  Path getPath2() {
    return Path()
      ..addPath(getTopRightPath2(), Offset(0, 0))
      ..addPath(getRightPath(), Offset(0, 0))
      ..addPath(getBottomRightPath1(), Offset(0, 0));
  }

  Path getPath3() {
    return Path()
      ..addPath(getBottomRightPath2(), Offset(0, 0))
      ..addPath(getBottomPath(), Offset(0, 0))
      ..addPath(getBottomLeftPath1(), Offset(0, 0));
  }

  Path getPath4() {
    return Path()
      ..addPath(getBottomLeftPath2(), Offset(0, 0))
      ..addPath(getLeftPath(), Offset(0, 0))
      ..addPath(getTopLeftPath1(), Offset(0, 0));
  }

  Path getTopPath() {
    return Path()
      ..moveTo(0 + radius, 0)
      ..lineTo(size.width - radius, 0);
  }

  Path getRightPath() {
    return Path()
      ..moveTo(size.width, 0 + radius)
      ..lineTo(size.width, size.height - radius);
  }

  Path getBottomPath() {
    return Path()
      ..moveTo(size.width - radius, size.height)
      ..lineTo(0 + radius, size.height);
  }

  Path getLeftPath() {
    return Path()
      ..moveTo(0, size.height - radius)
      ..lineTo(0, 0 + radius);
  }

  Path getTopRightPath1() {
    return Path()
      ..addArc(
        Rect.fromLTWH(size.width - (radius * 2), 0, radius * 2, radius * 2),
        vm.radians(-45),
        vm.radians(-45),
      );
  }

  Path getTopRightPath2() {
    return Path()
      ..addArc(
        Rect.fromLTWH(size.width - (radius * 2), 0, radius * 2, radius * 2),
        vm.radians(0),
        vm.radians(-45),
      );
  }

  Path getBottomRightPath1() {
    return Path()
      ..addArc(
        Rect.fromLTWH(size.width - (radius * 2), size.height - (radius * 2), radius * 2, radius * 2),
        vm.radians(45),
        vm.radians(-45),
      );
  }

  Path getBottomRightPath2() {
    return Path()
      ..addArc(
        Rect.fromLTWH(size.width - (radius * 2), size.height - (radius * 2), radius * 2, radius * 2),
        vm.radians(90),
        vm.radians(-45),
      );
  }

  Path getBottomLeftPath1() {
    return Path()
      ..addArc(
        Rect.fromLTWH(0, size.height - (radius * 2), radius * 2, radius * 2),
        vm.radians(135),
        vm.radians(-45),
      );
  }

  Path getBottomLeftPath2() {
    return Path()
      ..addArc(
        Rect.fromLTWH(0, size.height - (radius * 2), radius * 2, radius * 2),
        vm.radians(180),
        vm.radians(-45),
      );
  }

  Path getTopLeftPath1() {
    return Path()
      ..addArc(
        Rect.fromLTWH(0, 0, radius * 2, radius * 2),
        vm.radians(225),
        vm.radians(-45),
      );
  }

  Path getTopLeftPath2() {
    return Path()
      ..addArc(
        Rect.fromLTWH(0, 0, radius * 2, radius * 2),
        vm.radians(270),
        vm.radians(-45),
      );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

答案 1 :(得分:1)

Ajil的解决方案看起来是正确的方向。没错,对于这么小的事情来说,这是一件了不起的工作。

这是我的单调乏味的工作,需要对其进行整理以简化它,但是它可以更准确地显示您的图像。

class Example extends StatefulWidget {
  @override
  _ExampleState createState() {
    return _ExampleState();
  }
}

class _ExampleState extends State<Example> {
  double height;
  double width;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    height = MediaQuery.of(context).size.height;
    width = MediaQuery.of(context).size.width;

    const double pi = 3.1415926535897932;
    return Scaffold(
        appBar: AppBar(
          title: Text("Some project"),
        ),
        body: Center(
          child:
              Container(
                  width: width = 350,
                  height: height = 300,
                  child: Stack(children: <Widget>[
                    Align(
                        alignment: Alignment.center,
                        child: Container(
                          width: 300,
                          height: 300,
                          // color: Colors.red,
                          child: FlatButton(
                              onPressed: () {
                                print('button pressed');
                              },
                              child: Text(
                                'BUTTON',
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: 20, fontWeight: FontWeight.bold),
                              )),
                        )),
                    Align(
                      //TOP BORDER
                      alignment: Alignment.topCenter,
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.blue[200],
                          border:
                              Border.all(width: 3.0, color: Colors.transparent),
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(10.0),
                              topRight: Radius.circular(10.0)),
                        ),
                        height: 50,
                        width: width,
                      ),
                    ),
                    Align(
                      //LEFT BORDER
                      alignment: Alignment.topLeft,
                      child: Container(
                        height: height,
                        width: 30,
                        decoration: BoxDecoration(
                            color: Colors.blue[100],
                            border: Border.all(
                                width: 3.0, color: Colors.transparent),
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(10.0),
                                bottomLeft: Radius.circular(10.0))),
                      ),
                    ),
                    Align(
                      //RIGHT BORDER
                      alignment: Alignment.topRight,
                      child: Container(
                        height: height,
                        width: 15,
                        decoration: BoxDecoration(
                            color: Colors.blue[300],
                            border: Border.all(
                                width: 3.0, color: Colors.transparent),
                            borderRadius: BorderRadius.only(
                                topRight: Radius.circular(10.0),
                                bottomRight: Radius.circular(10.0))),
                      ),
                    ),
                    Align(
                      //BOTTOM BORDER
                      alignment: Alignment.bottomCenter,
                      child: Container(
                        height: 15,
                        width: double.infinity,
                        decoration: BoxDecoration(
                            color: Colors.blue,
                            border: Border.all(
                                width: 3.0, color: Colors.transparent),
                            borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(10.0),
                                bottomRight: Radius.circular(10.0))),
                      ),
                    ),
                    Align(
                      //TOPLEFT BORDER
                      alignment: Alignment.topLeft,
                      child: Container(
                        decoration: BoxDecoration(
                          gradient: SweepGradient(
                              center: FractionalOffset.bottomRight,
                              startAngle: pi * 1.349,
                              endAngle: pi * 1.35,
                              colors: [
                                Colors.blue[100],
                                Colors.blue[200],
                              ]),
                          border:
                              Border.all(width: 3.0, color: Colors.transparent),
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(10.0),
                          ),
                        ),
                        width: 30,
                        height: 50,
                      ),
                    ),
                    Align(
                        //TOP RIGHT BORDER
                        alignment: Alignment.topRight,
                        child: Container(
                          decoration: BoxDecoration(
                              gradient: SweepGradient(
                                  center: FractionalOffset.topRight,
                                  startAngle: 1.86,
                                  endAngle: 1.87,
                                  colors: [
                                    Colors.blue[300],
                                    Colors.blue[200],
                                  ]),
                              border: Border.all(
                                  width: 3.0, color: Colors.transparent),
                              borderRadius: BorderRadius.only(
                                topRight: Radius.circular(10.0),
                              )),
                          width: 15,
                          height: 50,
                        )),
                    Align(
                      //BOTTOMLEFT BORDER
                      alignment: Alignment.bottomLeft,
                      child: Container(
                        decoration: BoxDecoration(
                          gradient: SweepGradient(
                              center: FractionalOffset.bottomLeft,
                              startAngle: 5.8,
                              endAngle: 5.81,
                              colors: [
                                Colors.blue[100],
                                Colors.blue,
                              ]),
                          border:
                              Border.all(width: 3.0, color: Colors.transparent),
                          borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(10.0),
                          ),
                        ),
                        width: 30,
                        height: 15,
                      ),
                    ),
                    Align(
                        //BOTTOM RIGHT BORDER
                        alignment: Alignment.bottomRight,
                        child: Container(
                          decoration: BoxDecoration(
                              gradient: SweepGradient(
                                  center: FractionalOffset.bottomRight,
                                  startAngle: 3.99,
                                  endAngle: 4,
                                  colors: [
                                    Colors.blue,
                                    Colors.blue[300],
                                  ]),
                              border: Border.all(
                                  width: 3.0, color: Colors.transparent),
                              borderRadius: BorderRadius.only(
                                bottomRight: Radius.circular(10.0),
                                // bottomRight: Radius.circular(10.0)
                              )),
                          width: 15,
                          height: 15,
                        ))
                  ])),
        ));
  }
}

How it looks