Flutter Stuff

How to Open New Page and Back without Context in Flutter

**How to Open a New Page and Go Back Without Context in Flutter**

Are you building a Flutter app and struggling to navigate between pages? Do you want to open a new page and easily go back to the previous one? Well, you’re in luck! In this blog post, we’ll show you how to open a new page and go back without context in Flutter.

**What is Context?**

Before we dive into the solution, let’s quickly talk about what context is in Flutter. In Flutter, context refers to the current situation or environment of an application. It’s an object that provides information about the widget tree and allows you to retrieve data from it.

In the context of navigation, context is used to access the current page and navigate to a new one. However, when you need to open a page without any context, it can be tricky.

**The Problem**

Suppose you have a login page that, once a user logs in, opens a new page. You want to user to be able to go back to the login page without having to manually navigate back. But, you can’t use context to navigate because there is no context available.

**The Solution**

To open a new page and go back without context, you can use the `Navigator` class in Flutter. Here’s how:

1. First, create a new page and add it to the navigation stack using the `Navigator.push` method:
“`dart
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewPage())
);
“`
2. To go back to the previous page, you can use the `Navigator.pop` method:
“`dart
Navigator.pop(context);
“`
3. However, as we mentioned earlier, you can’t use context when there is no context available. So, you need to create a way to access the `Navigator` instance without context.

**The Hack**

Here’s a clever hack to get around this problem. You can create a global `Navigator` instance using a `Provider` widget:
“`dart
class NavigatorProvider with ChangeNotifier {
late final GlobalKey _navigatorKey;

NavigatorProvider() {
_navigatorKey = GlobalKey();
}

Future pushPage({required Widget page}) async {
await _navigatorKey.currentState!.push(
MaterialPageRoute(builder: (context) => page)
);
}

Future popPage() async {
await _navigatorKey.currentState!.pop();
}
}
“`
Then, you can use this `NavigatorProvider` widget in your app:
“`dart
ChangeNotifierProvider(
create: (_) => NavigatorProvider(),
child: MyApp(),
)
“`
And finally, you can access the `Navigator` instance from anywhere in your app using the `Provider` widget:
“`dart
Consumer(
builder: (context, provider, child) {
return ElevatedButton(
onPressed: () async {
await provider.pushPage(page: NewPage());
},
child: Text(‘Open new page’),
);
},
)
“`
**Conclusion**

In this blog post, we showed you how to open a new page and go back without context in Flutter. We used the `Navigator` class and a clever hack involving a `Provider` widget to get around the problem.

By following these steps, you’ll be able to navigate between pages easily and efficiently in your Flutter app. Happy coding!

Leave a Comment

Scroll to Top