【问题标题】:How to coloring specific edge of Container border in flutter?如何在颤动中为容器边框的特定边缘着色?
【发布时间】:2020-12-18 16:32:31
【问题描述】:

大家好,我只想为容器的右上角和左下角着色 我没有找到使用颤振的方法

Container( 
          child: Text("Your Text"),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10.0),

          ),
          boxShadow: [
              BoxShadow(
                color: primaryColor.withOpacity(0.2),
                spreadRadius: 4,
                blurRadius: 7,
                offset: Offset(0, 3), // changes position of shadow
              ),

            ],
        ),

下图描述了我想要实现的目标。 请帮助我,从两天前开始我就一直坚持下去

【问题讨论】:

  • 你需要一个自定义的Decoration,类似于gist.github.com/pskink/…
  • 非常感谢兄弟。我使用了 CornerDecoration 类,但我想制作圆角我怎样才能得到它或者我应该在哪里编辑以获得我需要的东西
  • 请看结果:i.ibb.co/ZTht3PV/stroke-flutter.jpg我想让它变成圆形边框我应该编辑什么来做?
  • 你必须改变paint方法
  • 我需要改变什么我没有很好的经验,所以如果你指导我,我会很高兴提前谢谢

标签: flutter dart


【解决方案1】:

您可以尝试的一种方法是使用CustomPainter。这里我们先画一个圆角的普通矩形。然后在上面用红色重新绘制 2 个角。

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      height: 100,
      child: CustomPaint(
        painter: MyPainter(),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    
    double w = size.width;
    double h = size.height;
    double r = 15;                      //<-- corner radius
    
    Paint blackPaint = Paint()
      ..color = Colors.black
      ..style = PaintingStyle.stroke
      ..strokeWidth = 5;

    Paint redPaint = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.stroke
      ..strokeWidth = 5;
    
    RRect fullRect = RRect.fromRectAndRadius(
          Rect.fromCenter(center: Offset(w/2, h/2), width: w, height: h),
          Radius.circular(r),
        );
    
    Path topRightArc = Path()
      ..moveTo(w - r, 0)
      ..arcToPoint(Offset(w, r), radius: Radius.circular(r));
    
    Path bottomLeftArc = Path()
      ..moveTo(r, h)
      ..arcToPoint(Offset(0, h - r), radius: Radius.circular(r));
    
    canvas.drawRRect(fullRect, blackPaint);
    canvas.drawPath(topRightArc, redPaint);
    canvas.drawPath(bottomLeftArc, redPaint);
  }

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

结果:

【讨论】:

猜你喜欢
  • 2016-06-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多