- Customizing Fonts in Flutter with ExampleFlutter Skeleton Text with ExampleFlutter Themes with ExampleFlutter Lazy Loader with ExampleFlutter UI Orientation with ExampleFlutter Animation in Route Transition with ExampleFlutter Physics Simulation in Animation with ExampleFlutter Radial Hero Animation with ExampleFlutter Hinge Animation with ExampleFlutter Lottie Animation with Example
- URLs in Flutter with ExampleRoutes and Navigator in FlutterFlutter WebSockets with ExampleFlutter Named Routes with ExampleFlutter Arguments in Named RoutesMulti Page Applications in FlutterFlutter Updating Data on the InternetFlutter Fetching Data From the InternetFlutter Deleting Data On The InternetFlutter Sending Data To The InternetFlutter Send Data to Screen with Example
Flutter Arguments in Named Routes
In Flutter, you can pass arguments between screens using named routes. This allows you to pass data between screens and customize the behavior of each screen based on the data it receives.
Here's an example of how to pass arguments between screens using named routes in Flutter
Define your named routes and their corresponding screens in your MaterialApp
widget in main.dart
import 'package:flutter/material.dart';
import 'package:flutter_blog/screens/home_screen.dart';
import 'package:flutter_blog/screens/post_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Blog',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/post': (context) => PostScreen(),
},
);
}
}
In this example, we have defined two named routes:
'/'
for the home screen and '/post'
for the post screen.Pass arguments to a named route using Navigator.pushNamed()
and arguments
:
Navigator.pushNamed(
context,
'/post',
arguments: {'post': post},
);
In this example, we are passing a post object as an argument to the post screen using the
'/post'
route.Receive arguments from a named route using ModalRoute.of()
and settings.arguments
:
class PostScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
final post = args['post'];
return Scaffold(
appBar: AppBar(
title: Text('Flutter Blog'),
),
body: Center(
child: Text(post.title),
),
);
}
}
In this example, we are receiving a post object as an argument from the
'/'
route and displaying its title in the post screen.Run your app and test passing arguments between screens:
flutter run
This is just a basic example of how to pass arguments between screens using named routes in Flutter. You can customize the arguments to fit your app's specific needs, such as passing multiple arguments or using different data types.