Flutter learn three named routes

Project structure

 

 

 All routes in the unified management Routes.dart

Routes.dart

All interfaces will need to jump the introduction come in, onGenerateRoute wording unchanged, you can copy directly,

Meaning probably get the name of the page you want to jump in, if not empty check if any parameters, if a book is not empty execution

pageContentBuilder (context, arguments: settings.arguments) ) ;, 
If the argument is empty, execution
 pageContentBuilder(context));
Import 'Package: Flutter / material.dart' ; 

Import '../pages/Tabs.dart' ;
 Import '../pages/Form.dart' ;
 Import '../pages/Search.dart'; // Configure routing 
Final routes = {
       '/' :( context) => Tabs (),
       '/ form' :( context) => FormPage (),
       '/ Search' :( context, arguments {}) => SearchPage to (arguments: arguments), 
}; 

// fixed wording 
var onGenerateRoute = (RouteSettings Settings) {
       // unitary 
      Final String name = settings.name; 
       Final Function pageContentBuilder = routes[name];
      if (pageContentBuilder != null) {
        if (settings.arguments != null) {
          final Route route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context, arguments: settings.arguments));
          return route;
        }else{
            final Route route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context));
            return route;
        }
      }
};

main.dart

In the main, this code is small, roughly meaning at startup, by default Tabs.dart 

 

 

 Then bind onGenerateRoute

Import 'Package: Flutter / material.dart' ;
 Import 'routes / Routes.dart' ; 

void main () => RunApp (the MyApp ());
 class the MyApp the extends StatelessWidget {   
  
  @override 
  the Widget Build (BuildContext context) { 
    return MaterialApp (
       // Home: Tabs (),    
      initialRoute: '/',      // routing initialization of loading 
      onGenerateRoute: onGenerateRoute 
     
    ); 
  } 
}

 

Use when we do not need to pass the value of Just delete it like, when needed, plus traditional values ​​like

But the jump in the past need to write a page to get the constructor

arguments: {
                "id":123
              }
        RaisedButton ( 
            Child: the Text ( "search jump to the page" ), 
            onPressed: () { 
              // routing jump 
              Navigator.pushNamed (context, '/ Search' , arguments: {
                 "ID": 123 
              }); 
            }, 
            Color : Theme.of (context) .accentColor, 
            textTheme: ButtonTextTheme.primary 
        ),

Jump here is the search page, search page would look at how to write it

import 'package:flutter/material.dart';

class SearchPage extends StatelessWidget {
  
  final arguments;
  SearchPage({this.arguments});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        title: Text("搜索页面"),
      ) ,
      body: Text("搜索页面内容区域${arguments != null ? arguments['id'] : '0'}"),
    );
  }
}

It can be seen in the above arguments we define a

When the acquired [ 'id'] can be acquired by the arguments to pass over a page id values, thus substantially

Click here to view the source code is not clear

Guess you like

Origin www.cnblogs.com/inthecloud/p/11832701.html