operator + method

Box operator +(
  1. Object? obj
)

You can create boxes by adding a Box to one these types: bool, Color, EdgeInsetsGeometry, AlignmentGeometry, or Widget.

Examples:

// To hide the box:
Box(...) + false;

// To show the box:
Box(...) + true;

// To change the box color:
Box(...) + Colors.green;

// To change the box padding:
Box(...) + Pad(all: 10);

// To substitute the box child:
Box(...) + Text('abc');

// To put a box inside of another:
Box(...) + Box(...);

Note: If you add null, that's not an error. It will simply return the same Box. However, if you add an invalid type it will throw an error in RUNTIME.

Implementation

Box operator +(Object? obj) {
  if (obj == null) return this;

  bool isBool = obj is bool;
  bool isColor = obj is Color;
  bool isEdgeInsetsGeometry = obj is EdgeInsetsGeometry;
  bool isAlignmentGeometry = obj is AlignmentGeometry;
  bool isWidget = obj is Widget;

  if (!isBool && !isColor && !isEdgeInsetsGeometry && !isAlignmentGeometry && !isWidget)
    throw ArgumentError("Can't add Box to ${obj.runtimeType}.");
  else
    return copyWith(
      show: isBool ? obj : null,
      color: isColor ? obj : null,
      padding: isEdgeInsetsGeometry ? obj : null,
      alignment: isAlignmentGeometry ? obj : null,
      child: isWidget ? obj : null,
    );
}