Routing with settings from URL-querystring or arguments from pustNamed
URL example: domainname/secondpage?firstname=Lou&lastname=Reed
Pagetitle in browser is defined with the help of the Title widget or with the custom made function: setPageTitle()
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_strategy/url_strategy.dart';
void main() {
setPathUrlStrategy();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
/*
initialRoute: '/',
routes: {
HomePage.routeName: (context) => HomePage(),
SecondPage.routeName: (context) => SecondPage(),
},
*/
onGenerateRoute: generateRoute,
onUnknownRoute: (settings) {
return MaterialPageRoute(builder: (context) => HomePage());
},
);
}
}
Route<dynamic> generateRoute(RouteSettings settings) {
final Map parameters = Uri.base.queryParametersAll;
final arguments = settings.arguments;
print('Parametre: $parameters');
print('Arguments: $arguments');
final List<String> uri = settings.name!.split('?');
//Write url queryparameters or settings-arguments to Routesettings - or null
final RouteSettings? arg = (parameters.length > 0 || arguments != null)
? RouteSettings(arguments: (arguments != null) ? arguments : parameters)
: null;
switch (uri[0]) {
case HomePage.routeName:
return MaterialPageRoute(
builder: (context) => HomePage(),
settings: arg,
);
case FirstPage.routeName:
return MaterialPageRoute(
builder: (context) => FirstPage(),
settings: arg,
);
case SecondPage.routeName:
return MaterialPageRoute(
builder: (context) => SecondPage(),
settings: arg,
);
default:
return MaterialPageRoute(builder: (context) => HomePage());
}
}
/// Homepage page
class HomePage extends StatelessWidget {
static const String routeName = '/';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Title(
title: 'Homepage',
color: Colors.green,
child: ElevatedButton(
onPressed: () => Navigator.of(context).pushNamed(
FirstPage.routeName,
arguments: {'my_argument': '47'},
),
child: Text('move to Firstpage with an argument'),
)),
);
}
}
/// First page
class FirstPage extends StatelessWidget {
static const routeName = '/firstpage';
@override
Widget build(BuildContext context) {
setPageTitle('Firstpage', context);
var arguments = ModalRoute.of(context)?.settings.arguments;
return Scaffold(
body: Text('Firstpage | arguments: $arguments'),
);
}
}
/// Seconde page
class SecondPage extends StatelessWidget {
static const routeName = '/secondpage';
@override
Widget build(BuildContext context) {
setPageTitle('Secondpage', context);
var arguments = ModalRoute.of(context)?.settings.arguments;
return Scaffold(
body: Text('Secondpage | arguments: $arguments'),
);
}
}
/// Set title of page in browser
void setPageTitle(String title, BuildContext context) {
SystemChrome.setApplicationSwitcherDescription(ApplicationSwitcherDescription(
label: title,
primaryColor: Theme.of(context).primaryColor.value, // This line is required
));
}
