WayFar
Search…
Navigation and routes

Routing

When you add a new screen, you have to register it in ROUTES static class, in the file lib/core/navigation/routes.dart :
routes.dart
1
class ROUTES {
2
static final String Home = "/";
3
static final String Example = "/example";
4
...
5
}
Copied!
Now map it to the corresponding widget (screen) using the registerRoutes function :
routes.dart
1
Map<String, WidgetBuilder> registerRoutes() {
2
return <String, WidgetBuilder>{
3
ROUTES.Home: (context) => AuthGuard(context, Home()),
4
ROUTES.Examle: (context) => ExampleScreen(),
5
...
6
};
7
}
Copied!
If the screen needs arguments, use the function onGenerateAppRoute to pass parameters to the screen :
routes.dart
1
Route onGenerateAppRoute(RouteSettings settings) {
2
var route = settings.name;
3
if (route==ROUTES.ParkingDetails) {
4
// Read arguments from RouteSettings
5
RouteArgs<String> args = settings.arguments as RouteArgs<String>;
6
return MaterialPageRoute(
7
builder: (context) {
8
return AuthGuard(context, ParkingDetailScreen(
9
parkingId: args.value,
10
));
11
},
12
);
13
}
14
Copied!

Protecting routes

To protect routes from unauthorized access, an authentication guard has been implemented (AuthGuard).
If the user is not authenticated, the guard redirect it to the sign in screen, otherwise the access is granted :
auth_guard.dart
1
Widget AuthGuard(BuildContext context, Widget widget) {
2
return BlocBuilder<AuthenticationBloc, AuthenticationState>(
3
bloc: resolve<AuthenticationBloc>(),
4
builder: (BuildContext context, state) {
5
if (state is Uninitialized) return Scaffold(body: Container());
6
return (state is Authenticated) ? widget : SignInScreen();
7
},
8
);
9
}
Copied!

Drawer menu

To be able to add or edit items form the application drawer menu, head over to the file menu.dart under lib/core/navigation.
You can add a record by giving it a key, an icon and the corresponding route :
menu.dart
1
final menu = [
2
{
3
"key": "home",
4
"icon": FontAwesomeIcons.home,
5
"route": ROUTES.Home,
6
},
7
...
8
]
Copied!