The so-called Route Management is to manage how to jump between pages. It is usually also called navigation management. Route Management in Flutter is similar to native development. Whether it is Android or iOS, navigation management will maintain a route stack. The route push operation corresponds to opening a new page, and the route pop operation corresponds to the page closing operation. Route Management mainly refers to how to manage the route stack.
2. MaterialPageRoute
2.1 Basic definition
MaterialPageRoute is a component provided by the Material component library. It can implement routing switching animations consistent with the platform page switching animation style for different platforms:
For Android, when a new page is opened, the new page will slide from the bottom of the screen to the top of the screen; when the page is closed, the current page will slide from the top of the screen to the bottom of the screen and then disappear, while the previous page will be displayed on the screen.
For iOS, when a page is opened, the new page will slide from the right edge of the screen to the left side of the screen until the new page is fully displayed on the screen, while the previous page will slide from the current screen to the left side of the screen and disappear; when the page is closed, just the opposite, the current page will slide out from the right side of the screen, and the previous page will slide in from the left side of the screen.
builder is a WidgetBuilder callback that constructs the route's content and returns a widget. Typically, this callback returns an instance of the new route.
settings Contains the configuration information of the route, such as the route name and whether it is the initial route (home page).
maintainState By default, when a new route is pushed into the stack, the original route will still be saved in memory. If you want to release all the resources occupied by the route when it is no longer used, you can set maintainState to false.
fullscreenDialog indicates whether the new route is presented as a full-screen modal. On iOS, when fullscreenDialog is true, the page slides in from the bottom of the screen rather than horizontally.
canUpdate(...) is a static method used primarily to reuse existing widgets when the widget tree is rebuilt.
The basic function: whether to use the new widget object to update the configuration of the corresponding Element object in the old UI tree.
Judgment rule: As long as newWidget and oldWidget’s runtimeType and key are equal at the same time, new widget updates the configuration of the Element object, otherwise a new Element will be created.
1.2 Four trees in Flutter
1.2.1 Basic Contacts and Responsibilities
Generates an Element tree based on the Widget tree. The nodes in the Element tree all inherit from the Element class.
Generates a Render tree (rendering tree) based on the Element tree. The nodes in the rendering tree all inherit from the RenderObject class.
Generates a Layer tree based on the rendering tree, and then displays it on the screen. The nodes in the Layer tree all inherit from the Layer class.
Layout and rendering logic is in the Render tree.
Element is the glue between Widget and RenderObject.
1.2.2 Example
1 2 3 4 5 6 7 8 9
Container( // A container widget color: Colors.blue, // Set container background color child: Row( // can arrange the children widget in the horizontal direction children: [ Image.network('https://www.example.com/1.png'), // Showing pictures of widget const Text('A'), ], ), );