buildDecoration method Null safety

Widget? buildDecoration(
  1. BuildMetadata meta,
  2. Widget child,
  3. {BoxBorder? border,
  4. BorderRadius? borderRadius,
  5. Color? color,
  6. bool isBorderBox = true}
)

Builds Decoration.

See https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing for more information regarding content-box (the default) and border-box (set isBorderBox to use).

Implementation

Widget? buildDecoration(
  BuildMetadata meta,
  Widget child, {
  BoxBorder? border,
  BorderRadius? borderRadius,
  Color? color,
  bool isBorderBox = true,
}) {
  if (border == null && borderRadius == null && color == null) {
    return child;
  }

  final container = child is Container ? child : null;
  final decoratedBox = child is DecoratedBox ? child : null;
  final prevDeco = container?.decoration ?? decoratedBox?.decoration;
  final baseDeco =
      prevDeco is BoxDecoration ? prevDeco : const BoxDecoration();
  final decoration = baseDeco.copyWith(
    border: border,
    borderRadius: borderRadius,
    color: color,
  );

  if (!isBorderBox || container != null) {
    return Container(
      decoration: decoration,
      child: container?.child ?? child,
    );
  } else {
    return DecoratedBox(
      decoration: decoration,
      child: decoratedBox?.child ?? child,
    );
  }
}