Box constructor Null safety

const Box(
  1. {Key? key,
  2. bool show = true,
  3. Color? color,
  4. EdgeInsetsGeometry? padding,
  5. double? width,
  6. double? height,
  7. AlignmentGeometry? alignment,
  8. Widget? child}
)

Box is something between a Container and a SizedBox. Unlike a Container it can be made const, so it's good for creating colored boxes, with or without padding:

const Box(color: Colors.red, width: 50, height:30, child: myChild);

The padding is only applied if the child is NOT NULL. If the child is null and width and height are also null, this means the box will occupy no space (will be hidden). Note: This will be extended in the future, so that it ignores horizontal padding when the child has zero width, and ignores vertical padding when the child has zero height.

You can also hide the box by making the show parameter equal to false.

Note: You can use the Pad class (provided in the assorted_layout_widgets package) for the padding, instead of EdgeInsets.

Debugging:

  • If need to quickly and temporarily add a color to your box so that you can see it, you can use the constructors Box.r for red, Box.g for green, and Box.b for blue.
  • If you want to see rebuilds, you can use the Box.rand constructor. It will then change its color to a random one, whenever its build method is called.

Implementation

const Box({
  Key? key,
  this.show = true,
  this.color,
  this.padding,
  this.width,
  this.height,
  this.alignment,
  this.child,
})  : _random = false,
      super(key: key);