Docs
Guides
Styling

Styling with Mix

Mix enhances Flutter development with a functional styling approach, focusing on separating visual semantics from business logic for improved readability and maintainability. Its emphasis on concise style declarations simplifies the workflow, allowing the creation of complex styles from simple building blocks.

This method fosters modularity, reusability, and flexibility in styling, streamlining the process of defining and applying styles to widgets.

Key Concepts

  • Functionality: The Style class is engineered for efficient handling and reuse of styling attributes and variants. Its design simplifies the application of uniform and reusable styles throughout your Flutter application.
  • Mechanism: It integrates various styling aspects, including colors, fonts, dimensions, and more, into a unified construct, streamlining the application of styles to widgets.
  • Advanced Capabilities: Beyond conventional styling attributes, Style also supports dynamic variants, enabling context-responsive and adaptable styling solutions.

Crafting Your Unique Style

Creating a Mix style is a seamless process. Start by constructing a Style object and incorporate your desired attributes:

final style = Style(
  $box.height(100),
  $box.width(100),
  $box.color.purple(),
  $box.borderRadius(10),
);
⚠️

Important Note: Remember that the sequence of attributes is crucial in their composition and overriding. Styling attributes merge sequentially, with subsequent attributes taking precedence.

Composing Styles

Create styles by combining and merging multiple Style instances. This technique, called style composition, allows you to build complex styles while ensuring modularity, reusability, flexibility, and maintainability in your codebase. The secret lies in calling the Mix class and passing attributes as arguments.

Mixing Attributes

Simply use the add() or addAll() method on a Style instance. For example:

final baseStyle = Style(
  $box.height(100),
  $box.width(100),
  $box.color.purple(),
  $box.borderRadius(10),
);
 
final newStyle = baseStyle.add(
  $box.border.width(2),
  $box.border.color.black(),
);

In this example, the newStyle inherits all attributes from the baseStyle, and add border.width and border.color attributes to it.

Overriding Attributes

Taking Control You can also override specific attributes of a style by passing new values to the mix() method. For instance:

final newStyle = baseStyle.add(
  $box.color.blue(),
);

In this example, the newStyle will inherit all attributes from the baseStyle, except for the box.color attribute, which will be overridden by the new value.

Combining Styles

You can also combine multiple styles into a single one.

final newStyle = Style.combine([
  baseStyle,
  otherStyle,
]);

Merging Styles

Use the merge() method on a Style instance. For example:

final otherStyle = Style(
  $box.border.width(2),
  $box.border.color.black(),
);
 
final newStyle = baseStyle.merge(otherStyle);

In this example, the newStyle will inherit all attributes from both the baseStyle and the otherStyle, with the otherStyle taking priority over the baseStyle in case of conflicts.

Applying Styles

To apply styles individually, to another style you can just call them inside the new style. This gives you control over their order.

In this example, the box.color attribute of the baseStyle is overridden by the box.color attribute of the newStyle:

final newStyle = Style(
  baseStyle(),
  $box.color.blue(),
);

However, if you call it after the $box.color attribute, the $box.color will be overridden by the baseStyle:

final newStyle = Style(
  $box.color.blue(),
  baseStyle(),
);

Benefits of Composition

  • Modularity: Break down complex styles into manageable chunks, which can be combined or merged as needed.
  • Reusability: Encourage the reuse of common style elements, reducing redundancy and maintaining consistency across the UI.
  • Flexibility: Dynamically adjust styles based on different conditions or user interactions.
  • Maintainability: Simplify the maintenance of styles, as changes can be made to individual mixes rather than entire stylesheets.