Slide 1

Slide 1 text

Flutter Navigations and routing Let’s learn how Flutter navigation system works By Ali Yazdi yazdi.dev linkedin.com/in/aliyazdi75

Slide 2

Slide 2 text

Interrupt me if you have any questions, thanks!

Slide 3

Slide 3 text

“ Navigation is the key to get their heart! — Ali Yazdi

Slide 4

Slide 4 text

Most apps contain several screens for displaying different types of information. Navigation and routing screens = routes = widgets

Slide 5

Slide 5 text

Describe the current UI state Transitioning is done by framework Flutter is a declarative UI Declarative style simply call setState() + + + +

Slide 6

Slide 6 text

Declarative Vs. Imperative style // Imperative style b.setColor(red) b.clearChildren() ViewC c3 = new ViewC(...) b.add(c3) // Declarative style return ViewB( color: red, child: const ViewC(), );

Slide 7

Slide 7 text

Imperative Navigation Navigator.of(context).pushNamed('/login') @override Widget build(BuildContext context) { return MaterialApp( routes: { '/': (context) => Splash(), '/login': (context) => LoginPage(), ... }, ); }

Slide 8

Slide 8 text

Navigator.of(context).pop() Imperative Navigation

Slide 9

Slide 9 text

What is Go? Accessing the web app through url path

Slide 10

Slide 10 text

Removing a route underneath in the stack Changing routes stack based on app state + No stack (Go) = no result data Browser forward button Limitations + + + Inconsistent experience using the browser’s back and forward buttons +

Slide 11

Slide 11 text

Navigator 2.0 Declarative Way

Slide 12

Slide 12 text

Push and Pop + Go directly to the route Auth guard Change stack based on route state Scenarios + + + Browser’s back and forward buttons + 404 + Send back result data (Push, Go) + Send arguments to page +

Slide 13

Slide 13 text

Let’s see the sample gallery.yazdi.dev

Slide 14

Slide 14 text

Router Widget listens for routing information from OS ● Initial route provided on app startup ● A new route obtained when an intent is received ● A notification that the user hit the system back button

Slide 15

Slide 15 text

Navigator Widget A widget that manages a set of child widgets with a stack discipline. ● pages ● initialRoute ● onPopPage

Slide 16

Slide 16 text

RouteInformationParser RouteInformation: URI + State (Serializable) class BrowserState { List routesHistory; bool isUnauthenticated; bool isNotVerified; Gallery gallery; Media media; ... toJson(), fromJson() }

Slide 17

Slide 17 text

RouterDelegate A delegate that is used by the Router widget to build and configure a navigating widget. class AppRouterDelegate extends RouterDelegate with ChangeNotifier { @override Widget build(BuildContext context) { var browserState = routerState.browserState!; return AuthGuard( child: Navigator( pages: [...], onPopPage: (...) {...}, ), ); } } Defines app-specific behavior of how the Router learns about changes in app state and how it responds to them.

Slide 18

Slide 18 text

Navigation Structure

Slide 19

Slide 19 text

Example App Structure

Slide 20

Slide 20 text

Let’s see the code

Slide 21

Slide 21 text

Classes 2 3 4 1 RoutersState Routes Parser OuterRouterDelegate 5 InnerRouterDelegate Nested routers Able to add widget on all nested routes Routes for the destinations in a BottomAppBar Routes for a stack of views above it

Slide 22

Slide 22 text

medium.yazdi.dev github.com/aliyazdi75/gallery Want to learn more

Slide 23

Slide 23 text

THANK YOU! v