Flutter navigate to new page

In Flutter, you can navigate to a new page using the Navigator class. Here are a few ways to do it:

1. Using Navigator.push()

You can use Navigator.push() to push a new page onto the navigator's stack. Here's an example:

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => NewPage()),
);

In this example, NewPage is the new page you want to navigate to. The MaterialPageRoute is a built-in route that provides a platform-specific transition between the current page and the new page.

2. Using Navigator.pushNamed()

If you have defined a named route in your app's MaterialApp widget, you can use Navigator.pushNamed() to navigate to that route. Here's an example:

Navigator.pushNamed(context, '/new-page');

In this example, /new-page is the named route you want to navigate to.

3. Using Navigator.pushReplacement()

If you want to replace the current page with a new page, you can use Navigator.pushReplacement(). Here's an example:

Navigator.pushReplacement(
  context,
  MaterialPageRoute(builder: (context) => NewPage()),
);

In this example, the current page will be replaced with the new page.

4. Using Navigator.pushAndRemoveUntil()

If you want to navigate to a new page and remove all previous pages from the navigator's stack, you can use Navigator.pushAndRemoveUntil(). Here's an example:

Navigator.pushAndRemoveUntil(
  context,
  (Route<dynamic> route) => route.isFirst,
  (Route<dynamic> route) => NewPage(),
);

In this example, all previous pages will be removed from the navigator's stack, and the new page will be pushed onto the stack.

Remember to always use Navigator instances that are available in the widget tree, such as the Navigator instance provided by the MaterialApp widget or the Navigator instance provided by a Scaffold widget.