Docs
Widgets
Box

Box - The Styled Container

Box is a versatile and robust widget, crafted to streamline and enrich the styling process in Flutter. This widget is akin to Flutter's native Container, but it incorporates the advanced capabilities of the Mix styling system, offering a more nuanced and flexible approach to design.

Usage

Using Box is straightforward and efficient. You simply wrap your desired child widget with Box, and specify its styling through a Style object.

Box(
  style: Style(
    backgroundColor.blue(),
    borderRadius.all(8),
    padding.vertical(20),
    elevation(2),
  ),
  child: Text('Styled Box'),
);

In this example, Box adeptly applies a series of styles – a blue background, uniformly rounded corners, consistent vertical padding, and subtle elevation – to the enclosed Text widget.

Composition and Decoration

Mix excels in the composability of styles. Utilizing decorators such as scale, opacity, and rotate, you can add rich visual effects to your widgets, often with just a single line of code.

Box(
  style: Style(
    scale(1.2),
    opacity(0.75),
    rotate(45),
  ),
  child: Icon(Icons.star),
);

Utilities

The Box widget accepts a range of intuitive utilities, enhancing the ease of styling it. These utilities, functioning as style attribute builders, offer a streamlined approach to crafting intricate styles with minimal code. They cover a broad spectrum of styling aspects such as constraints, colors, and spacing, facilitating complex customizations and maintaining a cohesive design language across your application.

General

You can find all the Box utilities under the box utility. With this you can easily find all the utilities in one place, and this can be helpful when wanting to have better control over the types. However you can find some aliases for them to simplify the usage.

Constraints

  • width: Sets the width of the box.
  • height: Sets the height of the box.
  • maxWidth: Sets the maximum width the box can have.
  • minWidth: Sets the minimum width the box must have.
  • maxHeight: Sets the maximum height the box can have.
  • minHeight: Sets the minimum height the box must have.

Spacing

There are a few utilities make use of the SpacingUtility, like padding and margin. These utilities allow you to specify spacing in a variety of ways, including shorthand notation, individual sides, and horizontal and vertical axes.

padding

Padding creates space between the box's content and its border.

Shorthand

Allows quick, concise spacing definitions. The order and number of values dictate their mapping to the box sides.

// Uniform padding of 10 units on all sides.
padding(10)
// 10 units vertical (top and bottom), 20 units horizontal (left and right).
padding(10, 20)
// 10 units top, 20 units horizontal, 30 units bottom.
padding(10, 20, 30)
// 10 units top, 20 units right, 30 units bottom, 40 units left.
padding(10, 20, 30, 40)

Remember that you can always pass design tokens as values using the $space syntax. Read more about design tokens here.

The shorthand methods simplify the code, making it more readable and easier to write, while maintaining the versatile and powerful features that Mix provides.

Uniform Sides

Apply equal padding to all sides of the box.

padding.all(8);
Individual Sides

Two methods for individual side padding: direct method naming or the only method.

Using side name method

padding.top(8);
padding.bottom(8);
padding.left(8);
padding.right(8);
padding.start(8);
padding.end(8);

This approach is important because it also allows you easy access to design tokens for padding. For example, if you want to use the small design token for padding, you can do so like this:

padding.top.small();

Using only method

Apply padding to specific sides of the box. You can specify which sides to apply padding to by passing the corresponding parameters. You can also specify directional values like start and end for left and right padding.

padding.only(
    top: 16,
    bottom: 16,
    left: 8, // or start: 8,
    right: 8, // or end: 8,
);
Horizontal and Vertical

Define padding along horizontal or vertical axes.

padding.horizontal(8);
// padding.horizontal.small();
padding.vertical(8);
// padding.vertical.small();

margin

Margin controls the outer space of the Box. Mix provides a variety of options for defining margins.

Shorthand

Allows quick, concise spacing definitions. The order and number of values dictate their mapping to the box sides.

// Applies a uniform margin of 10 units on all sides.
margin(10)
// 10 units on top and bottom, 20 units on left and right sides.
margin(10, 20)
// 10 units top, 20 units horizontal, 30 units bottom.
margin(10, 20, 30)
// 10 units top, 20 units right, 30 units bottom, 40 units left.
margin(10, 20, 30, 40)

Shorthand methods make the code concise and readable, retaining the powerful customization options.

Uniform Sides

Set an equal margin to all sides:

margin.all(8);
Individual Sides

Two methods for individual side padding: direct method naming or the only method.

Using side name method

margin.top(8);
margin.bottom(8);
margin.left(8);
margin.right(8);
margin.start(8);
margin.end(8);

Access design tokens directly for margin, enhancing the ability to maintain consistent spacing throughout your app:

margin.top.small();

Using only method

Specify margins for individual sides:

margin.only(
    top: 16,
    bottom: 16,
    left: 8, // or start: 8,
    right: 8, // or end: 8,
);
Horizontal and Vertical

Define padding along horizontal or vertical axes.

margin.horizontal(8);
// margin.horizontal.small();
margin.vertical(8);
// margin.vertical.small();

Decoration

border

The border utility enables you to define the border styling for a Box widget. Through the use of BoxBorderUtility, developers can add precision and clarity in laying out borders on their UI components.

Uniform Border

Apply a border with equal style, width, and color on all four sides.

// Solid red border of 2 logical pixels on all sides.
border(
  color: Colors.red,
  width: 2,
  style: BorderStyle.solid,
  strokeAlign: 0.5,
)
 
// You can also use the .all method, which allows you a few benefits in syntax.
border.all(
  color: Colors.red,
  width: 2,
  style: BorderStyle.solid,
  strokeAlign: 0.5,
)
 
// You can also call directly its parameters like
border.all.color.red();
border.all.width(2);
border.all.style.solid();
border.all.strokeAlign(0.5);
Individual Sides

Apply borders to individual sides. This allows you to set different colors, widths, or styles for each side.

// Red border on the top side only.
border.top(
  color: Colors.red,
  width: 2,
  style: BorderStyle.solid,
  strokeAlign: 0.5,
);
// or border.bottom, border.left, border.right, border.start and border.end

Using individual sides border also allow you access parameter specific utilities.

border.top.color.red();
border.top.width(2);
border.top.style.solid();
border.top.strokeAlign(0.5);
Horizontal and Vertical

Apply border to either the horizontal or vertical sides of a Box. Useful for creating a border only on the top and bottom or on the sides.

// Borders on the left and right sides.
border.horizontal(
  color: Colors.green,
  width: 1,
  style: BorderStyle.solid,
);
// or border.vertical
 
// You can also use the .horizontal and .vertical methods, which allows you a few benefits in syntax.
border.horizontal.color.green();

borderRadius

The borderRadius utility is used for setting the border radius of UI elements in a Flutter application. This utility helps in defining how rounded the corners of an element should be, and it simplifies the application of border radius to widgets.

By providing a BorderRadiusGeometryUtility, this enables customization of each corner’s radius, offering a powerful and intuitive utility.

Utility API

Most methods of the borderRadius return a RadiusUtility, that allow more control over the radius.

// The callable method withing the RadiusUtility defaults a circular radius.
borderRadius.all(10.0);
 
// But you can also use other methods
borderRadius.all.circular(10.0);
 
// Allows to set x and y coordinates for the radius.
borderRadius.all.elliptical(10.0, 8.0);
 
// Sets the radius to Radius.zero
borderRadius.all.zero();
Uniform Border Radius

Sets a uniform radius for all corners:

// Sets a border radius of 10 logical pixels for all corners.
borderRadius.all(10.0);
Individual Corners

Specify the radius of individual corners, returning a BorderSideUtility for each corner:

// Sets a radius to bottomLeft corner.
borderRadius.bottomLeft(10.0);
 
// Sets a radius to topLeft corner.
borderRadius.topRight(10.0);

Also use the same for directional corners:

// Sets a radius to topStart corner.
borderRadius.topStart(10.0);
 
// Sets a radius to bottomEnd corner.
borderRadius.bottomEnd(10.0);
 
// Sets a radius to topEnd corner.
borderRadius.topEnd(10.0);
 
// Sets a radius to bottomStart corner.
borderRadius.bottomStart(10.0);
Specific Pairs or Axes

Convenient methods are provided for setting border radius to specific pairs of corners or axes:

// Sets radius of topLeft and topRight corners.
borderRadius.top(10.0);
 
// Sets radius of topLeft and bottomLeft corners.
borderRadius.left(10.0);
 
// Sets radius of topRight and bottomRight corners.
borderRadius.right(10.0);
 
// Sets radius of bottomLeft and bottomRight corners.
borderRadius.bottom(10.0);
  • backgroundColor: Sets the background color of the box.
  • elevation: Sets the elevation of the box.

Alignment

  • alignment: Sets the alignment of the box.

ClipBehavior

  • clipBehavior: Sets the clip behavior of the box.