push<T> function

Future<T?> push<T>({
  1. required BuildContext context,
  2. required Widget screen,
  3. bool pushUntil = false,
})

A Dart function that pushes a new route onto the navigation stack, displaying the specified screen widget.

Parameters:

  • context: the BuildContext associated with the current widget tree.
  • screen: a Widget representing the screen to be displayed.
  • pushUntil: an optional boolean value indicating whether the current route should be popped off the navigation stack before pushing the new route.

Returns: A Future that completes with an optional generic value when the route is popped and the user returns to the previous screen.

Example usage: // Pushes a new route onto the navigation stack, displaying the specified screen widget.

push<MyScreen>(context: context, screen: MyScreen(), pushUntil: true)

Implementation

Future<T?> push<T>({
  required BuildContext context,
  required Widget screen,
  bool pushUntil = false,
}) {
  if (pushUntil) {
    return Navigator.of(context).pushAndRemoveUntil<T?>(
        Platform.isAndroid
            ? MaterialPageRoute(builder: (_) => screen)
            : CupertinoPageRoute(builder: (_) => screen),
        (Route<dynamic> route) => false);
  }
  return Navigator.of(context).push<T>(
    Platform.isAndroid
        ? MaterialPageRoute(builder: (_) => screen)
        : CupertinoPageRoute(builder: (_) => screen),
  );
}