to<T> method

Future<T?>? to<T>(
  1. dynamic page, {
  2. bool? opaque,
  3. Transition? transition,
  4. Curve? curve,
  5. Duration? duration,
  6. int? id,
  7. String? routeName,
  8. bool fullscreenDialog = false,
  9. dynamic arguments,
  10. Bindings? binding,
  11. bool preventDuplicates = true,
  12. bool? popGesture,
  13. double gestureWidth(
    1. BuildContext context
    )?,
  14. ConditionalNavigation? condition,
})

Navigation.push() shortcut.

Pushes a new page to the stack

It has the advantage of not needing context, so you can call from your business logic

You can set a custom transition, and a transition duration.

You can send any type of value to the other route in the arguments.

Just like native routing in Flutter, you can push a route as a fullscreenDialog,

id is for when you are using nested navigation, as explained in documentation

If you want the same behavior of ios that pops a route when the user drag, you can set popGesture to true

If you're using the Bindings api, you must define it here

By default, GetX will prevent you from push a route that you already in, if you want to push anyway, set preventDuplicates to false

Example:

// Basic navigation
Get.to(() => HomePage());

// Conditional navigation based on login status
Get.to(
  () => HomePage(),
  condition: ConditionalNavigation(
    condition: () => AuthService.isLoggedIn,
    truePage: () => HomePage(),
    falsePage: () => LoginPage(),
  ),
);

// Conditional navigation with custom transition
Get.to(
  () => SettingsPage(),
  condition: ConditionalNavigation(
    condition: () => UserPermissions.hasAccess,
    truePage: () => SettingsPage(),
    falsePage: () => AccessDeniedPage(),
  ),
  transition: Transition.rightToLeft,
  duration: Duration(milliseconds: 500),
);

// Conditional navigation with arguments
Get.to(
  () => ProfilePage(),
  condition: ConditionalNavigation(
    condition: () => UserService.hasProfile,
    truePage: () => ProfilePage(),
    falsePage: () => CreateProfilePage(),
  ),
  arguments: {'userId': 123},
);

Implementation

Future<T?>? to<T>(
  dynamic page, {
  bool? opaque,
  Transition? transition,
  Curve? curve,
  Duration? duration,
  int? id,
  String? routeName,
  bool fullscreenDialog = false,
  dynamic arguments,
  Bindings? binding,
  bool preventDuplicates = true,
  bool? popGesture,
  double Function(BuildContext context)? gestureWidth,
  ConditionalNavigation? condition,
}) {
  if (condition != null) {
    page = condition.evaluate();
  }

  routeName ??= "/${page.runtimeType}";
  routeName = _cleanRouteName(routeName);
  if (preventDuplicates && routeName == currentRoute) {
    return null;
  }
  return global(id).currentState?.push<T>(
    GetPageRoute<T>(
      opaque: opaque ?? true,
      page: _resolvePage(page, 'to'),
      routeName: routeName,
      gestureWidth: gestureWidth,
      settings: RouteSettings(name: routeName, arguments: arguments),
      popGesture: popGesture ?? defaultPopGesture,
      transition: transition ?? defaultTransition,
      curve: curve ?? defaultTransitionCurve,
      fullscreenDialog: fullscreenDialog,
      binding: binding,
      transitionDuration: duration ?? defaultTransitionDuration,
    ),
  );
}