ShapeDecoration.fromBoxDecoration constructor Null safety

  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 to BoxShape.rectangle).


factory ShapeDecoration.fromBoxDecoration(BoxDecoration source) {
  final ShapeBorder shape;
  assert(source.shape != null);
  switch (source.shape) {
      if (source.border != null) {
        shape = CircleBorder(side: source.border!.top);
      } else {
        shape = const CircleBorder();
    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();
  return ShapeDecoration(
    color: source.color,
    image: source.image,
    gradient: source.gradient,
    shadows: source.boxShadow,
    shape: shape,