of<T> static method

ImplicitNavigatorState<T> of<T>(
  1. BuildContext context, {
  2. bool root = false,
})

Get the nearest ancestor ImplicitNavigatorState in the widget tree.

Implementation

static ImplicitNavigatorState<T> of<T>(
  BuildContext context, {
  bool root = false,
}) {
  ImplicitNavigatorState<T>? navigator;
  if (context is StatefulElement &&
      context.state is ImplicitNavigatorState<T>) {
    navigator = context.state as ImplicitNavigatorState<T>;
  }
  if (root) {
    navigator =
        context.findRootAncestorStateOfType<ImplicitNavigatorState<T>>() ??
            navigator;
  } else {
    navigator ??=
        context.findAncestorStateOfType<ImplicitNavigatorState<T>>();
  }
  return navigator!;
}