of static method

SpacingData of(
  1. BuildContext context
)

The data from the closest Spacing instance that encloses the given context.

Typical usage is as follows:

@override
Widget build(BuildContext context) {
  return SizedBox(
    height: Spacing.of(context).spaces.small,
  );
}

When the Spacing is actually created in the same build function (possibly indirectly, e.g. as part of a MaterialApp), the context argument to the build function can't be used to find the Spacing (since it's "above" the widget being returned). In such cases, the following technique with a Builder can be used to provide a new scope with a BuildContext that is "under" the Spacing:

@override
Widget build(BuildContext context) {
  return Spacing(
    data: SpacingData.fixed(10.0),
    body: Builder(
      // Create an inner BuildContext so that we can refer to
      // the Spacing with Spacing.of().
      builder: (BuildContext context) {
        return Center(
          child:SizedBox(
            height: Spacing.of(context).spaces.small,
          ),
        );
      },
    ),
  );
}

Implementation

static SpacingData of(BuildContext context) {
  final provider =
      context.dependOnInheritedWidgetOfExactType<_SpacingDataProvider>();
  assert(
    provider != null,
    'No Spacing in widget tree. Make sure that you declared a Spacing.',
  );
  return provider!.data;
}