In Flutter, passing data between screens (or routes) is a common task. This is essential for creating dynamic and interactive applications. In this section, we will explore different methods to pass data between screens in Flutter.
Key Concepts
- Navigator: The Navigator widget manages a stack of Route objects and provides methods for managing the stack, such as
pushandpop. - Routes: Routes are the screens or pages in your application. Each route is a widget.
- Arguments: Data passed between routes.
Methods to Pass Data
- Using Constructor Parameters
The simplest way to pass data between screens is by using constructor parameters.
Example
// First Screen
import 'package:flutter/material.dart';
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('First Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(data: 'Hello from First Screen'),
),
);
},
child: Text('Go to Second Screen'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
final String data;
SecondScreen({required this.data});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: Text(data),
),
);
}
}
- Using Named Routes with Arguments
Named routes allow you to define routes in a centralized place and pass arguments using the settings property.
Example
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
onGenerateRoute: (settings) {
if (settings.name == '/second') {
final args = settings.arguments as ScreenArguments;
return MaterialPageRoute(
builder: (context) {
return SecondScreen(
data: args.data,
);
},
);
}
assert(false, 'Need to implement ${settings.name}');
return null;
},
);
}
}
class ScreenArguments {
final String data;
ScreenArguments(this.data);
}
// First Screen
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('First Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(
context,
'/second',
arguments: ScreenArguments('Hello from First Screen'),
);
},
child: Text('Go to Second Screen'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
final String data;
SecondScreen({required this.data});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: Text(data),
),
);
}
}
- Using the
Navigator.push Method with Arguments
Navigator.push Method with ArgumentsYou can also pass data using the Navigator.push method by providing arguments directly.
Example
// First Screen
import 'package:flutter/material.dart';
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('First Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(),
settings: RouteSettings(
arguments: 'Hello from First Screen',
),
),
);
},
child: Text('Go to Second Screen'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final String data = ModalRoute.of(context)!.settings.arguments as String;
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: Text(data),
),
);
}
}Practical Exercise
Task
- Create a Flutter application with two screens:
HomeScreenandDetailScreen. - Pass a string message from
HomeScreentoDetailScreenusing one of the methods described above. - Display the passed message on
DetailScreen.
Solution
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailScreen(data: 'Hello from Home Screen'),
),
);
},
child: Text('Go to Detail Screen'),
),
),
);
}
}
class DetailScreen extends StatelessWidget {
final String data;
DetailScreen({required this.data});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Detail Screen')),
body: Center(
child: Text(data),
),
);
}
}Summary
In this section, we explored different methods to pass data between screens in Flutter:
- Using constructor parameters.
- Using named routes with arguments.
- Using the
Navigator.pushmethod with arguments.
Understanding these methods is crucial for building dynamic and interactive Flutter applications. In the next section, we will delve into deep linking, which allows users to navigate to specific parts of your app using URLs.
