自定义 ShapeBorder

6/9/2021 flutter

需要继承自 ShapeBorder

class SimpleShapeBoder extends ShapeBorder{
  
  EdgeInsetsGeometry get dimensions => null;

  
  Path getInnerPath(Rect rect, {TextDirection textDirection}) {
    return null;
  }

  
  Path getOuterPath(Rect rect, {TextDirection textDirection}) {
    return null;
  }

  
  void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
  }

  
  ShapeBorder scale(double t) {
    return null;
  }
}

# paint 方法

paint 中传递过来 Canvas 对象,和组件的区域。有了这俩货,算是可以为所欲为了吧


void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
    var paint = Paint()
        ..color = Colors.white
        ..strokeWidth = 2.0
        ..style = PaintingStyle.stroke
        ..strokeJoin = StrokeJoin.round;
    var w = rect.width;
    var h = rect.height;
    canvas.drawCircle(Offset(0.3*h,0.23*h), 0.12*h, paint);
    canvas.drawCircle(Offset(0.3*h,0.23*h), 0.06*h, paint..style=PaintingStyle.fill..color=Colors.black);
}

# getOuterPath 方法

返回一个 Path 对象,用于形状的裁剪


Path getOuterPath(Rect rect, {TextDirection textDirection}) {
    var path = Path();
    path.addRRect(RRect.fromRectAndRadius(rect, Radius.circular(10)));
    return path;
}
上次更新: 6/17/2021, 2:43:57 PM