ShapeDecoration.fromBoxDecoration constructor Null safety

ShapeDecoration.fromBoxDecoration(
  1. BoxDecoration source
)

Creates a shape decoration configured to match a BoxDecoration.

The BoxDecoration class is more efficient for shapes that it can describe than the ShapeDecoration class is for those same shapes, because ShapeDecoration has to be more general as it can support any shape. However, having a ShapeDecoration is sometimes necessary, for example when calling ShapeDecoration.lerp to transition between different shapes (e.g. from a CircleBorder to a RoundedRectangleBorder; the BoxDecoration class cannot animate the transition from a BoxShape.circle to BoxShape.rectangle).

Implementation

factory ShapeDecoration.fromBoxDecoration(BoxDecoration source) {
  final ShapeBorder shape;
  assert(source.shape != null);
  switch (source.shape) {
    case BoxShape.circle:
      if (source.border != null) {
        assert(source.border!.isUniform);
        shape = CircleBorder(side: source.border!.top);
      } else {
        shape = const CircleBorder();
      }
      break;
    case BoxShape.rectangle:
      if (source.borderRadius != null) {
        assert(source.border == null || source.border!.isUniform);
        shape = RoundedRectangleBorder(
          side: source.border?.top ?? BorderSide.none,
          borderRadius: source.borderRadius!,
        );
      } else {
        shape = source.border ?? const Border();
      }
      break;
  }
  return ShapeDecoration(
    color: source.color,
    image: source.image,
    gradient: source.gradient,
    shadows: source.boxShadow,
    shape: shape,
  );
}