How do I understand FLUTTER routing source code?| Developers say DTALK

Home > Sci-Tech

How do I understand FLUTTER routing source code?| Developers say DTALK

2022-01-28 18:04:44 32 ℃

This article original author: Nayuta, released in the original: Attack on Flutter

This issue Aspect:

70 lines of code to achieve a beggar version of the route

Source routing resolve details

Introduction

One day to see such a problem in the public No.

This question I cooked ah, just OverlayEntries translation and Routes rebuilt inside optimization mentioned, after the 1.17 version, when we open a new page (Route), the previous page will not be rebuilt.

Pam, I direct a link throw to, chic exit.

After a child I would look

(Awkward but polite smile) So in order to figure out a front page Why build, based on 1.12.13 I wrote a demo version of the test, and found that not only is the previous page will build again, in front of all the pages will be build.

Previous surface is covered by common sense should not be building up again the! What is happening? To clarify this question may really not so easy to analyze the plan in two phases principle. Benpian and we will analyze one of the most familiar stranger route from source.

First, the acquaintance route: an ability to switch page

Why is the first analysis of routing, because the problem occurs in the scene page switching.

Flutter we often through a line of code

To open a new page PageE, call Navigator.of (context) .pop exit a page. So routing simple terms, it is a page switching capacity.

Flutter how to achieve this capability? For a deeper understanding of the source code design, we issue a change in thinking, let us put aside the current routing mechanisms thinking: If the framework removed the routing mechanism, how would you implement page switching?

Second, how to implement a beggar version of the route

1, designed to manage the routing of the container and into the exit of each page, we can design a container to manage routing, then this container how to design? Page to open and close observation of these two processes, is actually very simple. Open the cover page is the goal on a page, and exit the process is just the opposite.

According to the existing Widget system we naturally think of Stack, Stack is relatively similar to the native layout, each Widget can be superimposed on the screen according to their position. As long as we have each of its child widget support full, then the Stack will show information for the last widget, which is not similar to a page every time you open it.

2, there is provided a page switching method based on Stack container so as to open and close the page is very simple. For you to a page we only need to add a new page to the pages in; shut down the page, we can simply remove the last one. In order to make the switching process smoother, you can add some animation transition effects.

In fact, just to open the page, for example three steps

Step 1, create a transition animation

Step 2, to add to the stack target page display

Step 3, open transition animation

Yes, you simply just need to complete these three steps, we can look at the results

Close to the turn.

3, let subpages use routing capabilities

We mentioned above to open and close the page method in routing a container, that sub-page how you can use this ability? Behind this question is actually Flutter in a very interesting topic, how the parent-child node data transfer?

We know Flutter framework there are three trees in the "Widget, Element, Render is how to form a tree structure? "They are familiar with the build process. Flutter provides several methods allow us to access parent-child nodes:

Source code for example, Navigator, MediaQuery, InheritedTheme, as well as many state regulatory frameworks we often use is achieved based on this principle. Also, by this method will provide routing capability to sub-pages.

You can view the full story:

Third, understand the source routing design

With the above thinking, the design of the source code we would be very clear. Now we go back to the use of routing

We designed the route contrast to disassemble principle.

Routing container: Navigator

Comparison of the two methods, in fact, we understand the role played Navigator is the route of the vessel. View source You will find that he was nested in MaterialApp, and internal Nagivator is achieved by Stack.

Child nodes each of our pages are Navigator, of course, you can go to get it through context.

Route: process transitions such as page design

After understand Navigator, we find that every time you open the page, it tends to need to pass PageRoute objects, which in turn play what role?

In our design, in order to make the transition nature, we have a transition animation to each page in the Open method. The animation, interaction block and other animation, interaction, etc. needed to switches in Flutter into the Route object. Through the form of hierarchical packages, these capabilities are realized by layers: After the previous thinking, look at the design of the route source, and the idea is very clear. For the learning of the source code, don't be in the details, think about the re-dismantling process from the whole, so that the way is in depth.

Fourth, billions of details in the source code

With the overall frame, we can concatenate the Navigator.Of (Context) .push process.

In OverLayRoute as follows:

Iterable createOverlayEntries sync * {/// to create a mask yield_modalBarrier = OverlayEntry (builder: _buildModalBarrier); /// to create the actual page content, the final call to the builder Route method yieldOverlayEntry (builder: _buildModalScope, maintainState: maintainState); }

The CreateoverLayenTries method in the first line of code will create a builder method that Zhe call to Route. Create the page we need to open with the mask, then add the entire collection to overlay (if you are not familiar with Overlay, you will be a stack .

Overlay method is also very simple, add the page to _Tries call setState update. This _ENTRIES simply looks like the Pages we designed in front, but there are more power to choose rendering, and we will analyze in detail later.

Five, summary

See this, I believe that your route from flutter will never feel unfamiliar, summarizing the key, there are three points:

Navigator as a routing container internally nested STACK's ability to provide page switching; via Context.FindrootanceStrateOFTYPE can access the parent node; ROUTE encapsulates the other capabilities you need when switching.

When we switch the page, the last page will take the following life cycles by default:

This is why? Do you have such a order? How can the flutter life cycle answer? We stayed in the next phase of analysis ~

See more developers exciting sharing

"Developers say DTALK" to collect products / technical content related to Chinese developers to collect Google Mobile Application (Apps & Games). Welcome everyone to share your experience or new discoveries in mobile applications, the experience or new discoveries in the mobile development process, and the application of the high-cost experience summary and the use of related products. We sincerely hope that Chinese developers who can give these outstanding Chinese developers will better show themselves, and give full play to their own specials. We will focus on the recommendation of the excellent cases throughout the technical content.