Reputation: 217
I currently have
Navigator.pushReplacementNamed(context, "/second", arguments: contact);
...
How would one rewrite this as a MaterialPageRoute
?
Contact contact = ModalRoute.of(context).settings.arguments;
.
'/second': (BuildContext context) => ViewContact(),
Upvotes: 2
Views: 4664
Reputation: 1
A bit late, but...
I believe u are looking for same approach to send params with MaterialPageRoute
as it's shown in tutorial with ModalRoute
.
If Route<dynamic>? generateNamedRoutes(RouteSettings settings) {
is u'r fun to generate routes, and u call this here:
MaterialApp(
navigatorKey: _navigatorKey,
onGenerateRoute: AppNavigator.generateNamedRoutes, // <-
...
then in generateNamedRoutes
, for navigation with params define:
switch (settings.name) {
case 'action/sequence_change':
route = MaterialPageRoute(
builder: (_) => const SequenceChangePage(), settings: settings);
In place from where u want to send params and perform navigation:
void _onEditAction() {
final sequence =
context.read<SequenceListCubit>().state.hightlightedSequence;
final args = SequenceChangeArguments(sequence: sequence);
AppNavigator.navigateWith(context,
to: AppNavRoute.actionSequenceChange, arguments: args); // <---
}
`AppNavRoute.actionSequenceChange` is for 'action/sequence_change'
where SequenceChangeArguments
- is your holder for params.
and func for navigation
static Future<T?> navigateWith<T extends Object?>(BuildContext context,
{required AppNavRoute to, Object? arguments}) {
return Navigator.of(context)
.pushNamed(AppNavigator.path(to), arguments: arguments);
}
While creating the widget u can now get u'r params
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)?.settings.arguments as SequenceChangeArguments;
final sequence = args.sequence; // <- required args
return BlocProvider(
create: (context) => SequenceChangeCubit(
context.read<BTAppSettingsRepository>(), sequence),
child: const SequenceChangeView());
}
Upvotes: 0
Reputation: 315
Yes We can do this by passing the name in the route setting parameter of material page route. Please refer to the following piece of code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VendorDetailScreen(widget.vendor),//pass any arguments
settings: RouteSettings(name: "vendorScreen")),//assign a name for the screen
In this way we can assign the name to the material page route. Now your navigation stack will have a route with the name "vendorScreen" after you push the screen.
Upvotes: 3
Reputation: 509
I think that you were asking for something like that:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ViewContact(contact),
),
);
Upvotes: 0
Reputation: 103
You can use a MaterialPageRoute
passing contact
as argument in this way:
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (BuildContext context) => ViewContact(contact)));
}
And then in the ViewContact page:
class ViewContact extends StatelessWidget {
Contact contact;
ViewContact(this.contact);
...
}
Upvotes: 0