Flutter Stuff

How to open Page Route in New Tab in Flutter Web

**How to Open Page Route in a New Tab in Flutter Web**

Hey there, fellow Flutter enthusiasts! Today, we’re going to dive into a super handy topic – how to open a page route in a new tab in Flutter Web. This is a feature that’s often needed in web applications, and I’m excited to show you how to achieve it in a step-by-step guide.

**Why Do We Need This Feature?**

Before we get started, let’s quickly discuss why we need to open a page route in a new tab. Imagine you’re building a web app that allows users to view different pages. You might want to open a particular page in a new tab when the user clicks on a button or link. This feature makes the user experience more seamless and intuitive.

**Getting Started**

To open a page route in a new tab, we’ll use a combination of the `Navigator` and `Uri` classes in Flutter. Let’s start by creating a new Flutter project and adding the `http` package to our `pubspec.yaml` file:
“`yaml
dependencies:
flutter:
sdk: flutter
http: ^0.13.3
“`
**Step 1: Create a New Tab**

First, we need to create a new tab. We can do this by using the `Router` widget and specifying the `href` property:
“`dart
Router(
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute(builder: (_) => NewTabPage());
},
)
“`
**Step 2: Open the New Tab**

Next, we need to open the new tab when the user clicks on a button or link. We can do this by using the `Uri` class to create a new URI and then using the `Navigator` class to navigate to that URI:
“`dart
ElevatedButton(
onPressed: () {
Uri uri = Uri.parse(‘#/new-tab’);
Navigator.of(context).push(UriStrategy(uri));
},
child: Text(‘Open New Tab’),
)
“`
**Step 3: Handle the New Tab**

Finally, we need to handle the new tab by creating a new route for the `NewTabPage` widget:
“`dart
MaterialApp(
routes: {
‘/new-tab’: (context) => NewTabPage(),
},
)
“`
**Putting it All Together**

Here’s the complete code snippet that opens a page route in a new tab:
“`dart
import ‘package:flutter/material.dart’;
import ‘package:http/http.dart’ as http;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘New Tab Demo’,
routes: {
‘/’: (context) => HomeScreen(),
‘/new-tab’: (context) => NewTabPage(),
},
);
}
}

class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Home Screen’),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Uri uri = Uri.parse(‘#/new-tab’);
Navigator.of(context).push(UriStrategy(uri));
},
child: Text(‘Open New Tab’),
),
),
);
}
}

class NewTabPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘New Tab’),
),
body: Center(
child: Text(‘This is the new tab page’),
),
);
}
}
“`
And that’s it! With these simple steps, you should now be able to open a page route in a new tab in your Flutter Web app. Happy coding!

Leave a Comment

Scroll to Top