of static method

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

Defaults to new ThemeData.fallback if there is no Theme in the given build context.

When the TimelineTheme is actually created in the same build function (possibly indirectly, e.g. as part of a Timeline), the context argument to the build function can't be used to find the TimelineTheme (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 TimelineTheme:

@override
Widget build(BuildContext context) {
  // TODO: replace to Timeline
  return TimelineTheme(
    data: TimelineThemeData.vertical(),
    child: Builder(
      // Create an inner BuildContext so that we can refer to the Theme with TimelineTheme.of().
      builder: (BuildContext context) {
        return Center(
          child: TimelineNode(
            direction: TimelineTheme.of(context).direction,
            child: Text('Example'),
          ),
        );
      },
    ),
  );
}

Implementation

static TimelineThemeData of(BuildContext context) {
  final inheritedTheme =
      context.dependOnInheritedWidgetOfExactType<_InheritedTheme>();
  return inheritedTheme?.theme.data ?? _kFallbackTheme;
}