Utilities topic

Utilities are classes with static functions and a series of global constants and functions for a given widget type that can be used to instantiate Attribute objects.

To use a utility class (BoxUtility, for this example) to create Attributes for backgroundColor and height of a Box widget, you could use the following code:

  Mix newMix = Mix(
    BoxUtility.backgroundColor(Colors.green),
    BoxUtility.height(20.0)
    );

Short Utils

The above works fine, but it's somewhat onerous to have to specify the utility class and full attribute name for every attribute you want to define.

Instead, Mix defines a set of global shortcut functions that you can use:

  Mix newMix = Mix(
    bgColor(Colors.green),
    h(20.0)
  );

Global functions are defined for each utility function, and will return an attribute of the correct type by virtue of the fact that utility shortcut names are unique.

(Note that the Short Util name is not necessarily the same as the static function name in the Utility class.)

Under The Covers

What is happening in the above code snippet can be traced in the source. BoxUtility shortcuts are defined in the file 'lib/src/attributes/box/box_short.utils.dart'. The ones we are using are declared thus:

  const bgColor = BoxUtility.backgroundColor;
  const h = BoxUtility.height;

These constants are assigned to the following static functions in the file 'lib/src/attributes/box/box.utils.dart':

    static BoxAttributes backgroundColor(Color color) =>
      BoxAttributes(color: color);

    static BoxAttributes height(double height) {
      return BoxAttributes(height: height);
    }

Note that each of these functions returns a BoxAttributes class, which is derived from Attributes.

In The API Docs

In each of the Utility classes, each of the static functions lists it's one or more applicable Short Util(s).

Using the above examples, the BoxUtility class page static methods for the attributes we are using are documented as follows:

Static Methods

...
backgroundColor(Color color) → BoxAttributes
Short Utils: bgColor
...
height(double height) → BoxAttributes
Short Utils: height, h