The article directories

    • Navigate to a new page and back
    • Navigate to a new page with parameters to return
    • Jumps with arguments through constructors
    • Carry parameter jumps through the RoutSettings
    • After routing
    • Pass a parameter to a specific route
    • Transition animation route Hero

Navigate to a new page and back

The Navigator. Push method

Appearance after jump

import 'package:flutter/material.dart'; Void main(){runApp(MaterialApp(title: 'basic route ', Home: FirstRoute(),)); } class FirstRoute extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('First Route'), ), body: Center( child: RaisedButton( child: Text('Open route'), onPressed: (){// Navigator. Push (Context, MaterialPageRoute(Builder: (context) => SecondRoute())); }),),); } } class SecondRoute extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('Second Route'), ), body: Center( child: RaisedButton( child: Text('Go back'), onPressed: (){ Navigator.pop(context); }),),); }}Copy the code

Navigate to a new page with parameters to return

import 'package:flutter/material.dart'; Void main() {runApp(MaterialApp(title: 'Return route with parameters ', Home: HomeScreen(),)); Class HomeScreen extends StatelessWidget {override Widget build(BuildContext context) {// TODO: implement build return Scaffold( appBar: AppBar( title: Text('Returning with data'), ), body: Center( child: SelectionButton(), ), ); } } class SelectionButton extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( onPressed: () { _navigateAndDisplaySelection(context); }, child: Text('Pick an option, any option! ')); Async {} _navigateAndDisplaySelection (BuildContext context) / / use the result to accept the second screen the return value of the final result = await the Navigator. Push ( context, MaterialPageRoute(builder: (context) => SelectionScreen()), ); // Cascade operator (..) Multiple functions can be called consecutively on the same object and member variables can be accessed. Using the cascade operator avoids creating the temporary variable scaffolding. Of (context).. removeCurrentSnackBar() .. showSnackBar(SnackBar(content: Text("$result"),)); Class SelectionScreen extends StatelessWidget {override Widget build(BuildContext context) {// TODO: implement build return Scaffold( appBar: AppBar( title: Text('Pick an option'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: Const edgeinsets. all(8.0), child: RaisedButton(onPressed: () {// Return "Yep" navigator. pop(context,'Yep!');}, child: Text('Yep'),),), paddING-back (padding-back: const EdgeInsets. All (8.0), child: RaisedButton(onPressed: Here () {/ / return "Noep" Navigator. Pop (context, 'Noep!');}, child: Text (' Noep),),),),),),); }}Copy the code

Jumps with arguments through constructors

import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; Void main() {runApp(MaterialApp(title: 'with parameters ', home:TodosScreen(todos: List.generate(20, (i)=> Todo( 'Todo $i', 'A description of what needs to be done for Todo $i' ))), )); } // Data class Todo {final String title; final String description; Todo(this.title, this.description); } class TodosScreen extends StatelessWidget { final List<Todo> todos; TodosScreen({Key key, @required this.todos}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('Todos'), ), body: ListView.builder( itemCount: todos.length, itemBuilder: (context, index) { return ListTile( title: Text(todos[index].title), onTap: () { Navigator.push(context, MaterialPageRoute( builder: (context) => DetailScreen(todo: todos[index]),)); }); })); }} // The constructor takes the class DetailScreen extends StatelessWidget{final Todo Todo; DetailScreen({Key key, @required this.todo}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text(todo.title),), body: Padding(Padding: edgeinsets.all (16.0),child: Text(todo.description),); }}Copy the code

Carry parameter jumps through the RoutSettings

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

void main() {
  runApp(MaterialApp(
    title: 'RouteSettings携参数跳转',
    home: TodosScreen(
        todos: List.generate(
            20,
            (i) => Todo('Todo $i',
                'A description of what needs to be done for Todo $i'))),
  ));
}

// 数据类(要携带的数据)
class Todo {
  final String title;
  final String description;

  Todo(this.title, this.description);
}

class TodosScreen extends StatelessWidget {
  final List<Todo> todos;

  TodosScreen({Key key, @required this.todos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Todos'),
      ),
      body: ListView.builder(
          itemCount: todos.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(todos[index].title),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => DetailScreen(),
                      //通过RouteSettings传递参数
                      settings: RouteSettings(
                        arguments: todos[index]
                      )
                    ));
              },
            );
          }),
    );
  }
}

//构造函数接受传递过来的参数
class DetailScreen extends StatelessWidget {

//  DetailScreen({Key key, @required this.todo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //通过ModalRoute取参数
    final Todo todo = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text(todo.title),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(todo.description),
      ),
    );
  }
}
Copy the code

After routing

import 'package:flutter/material.dart'; Void main() {runApp(MaterialApp(title: 'named route ', // When using initialRoute, you need to make sure that you do not also define the home property initialRoute: '/', routes: {// when we jump to "/", build FirstScreen "/" :(context) => FirstScreen(), // when we jump to "/second", Build SecondScreen '/second':(context) =>SecondScreen(),},); } class FirstScreen extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('First Screen'), ), body: Center( child: RaisedButton(onPressed: () {// When pressed jump to the second screen navigator.pushnamed (context, '/second'); }, child: Text('Launch screen'),), ), ); } } class SecondScreen extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('Second Screen'), ), body: Center( child: RaisedButton(onPressed: (){// Jump back to the first screen when pressed! Navigator.pop(context); },child: Text('Go back! '),),),); }}Copy the code

Pass a parameter to a specific route

import 'package:flutter/material.dart'; void main() => runApp(MyApp()); Class MyApp extends StatelessWidget {@override Widget Build (BuildContext Context) {return MaterialApp(// pass The onGenerateRoute() function takes the argument and passes it to the component, and routes are two ways to do this: (settings) { if (settings.name == PassArgumentsScreen.routeName) { final ScreenArguments arguments = settings.arguments;  return MaterialPageRoute(builder: (context) { return PassArgumentsScreen( title: arguments.title, message: arguments.message, ); }); } return null; }, the title: 'to specific routing and transfer' home: HomeScreen (), routes: {/ / components registered to the routing table ExtractArgumentsScreen routeName: (context) => ExtractArgumentsScreen(), }, ); } } class ScreenArguments { final String title; final String message; ScreenArguments(this.title, this.message); } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home Screen'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ RaisedButton( onPressed: () { Navigator.pushNamed(context, ExtractArgumentsScreen.routeName, arguments: }, child: Text('Navigate to screen that extracts arguments'), ), RaisedButton( onPressed: () { Navigator.pushNamed(context, PassArgumentsScreen.routeName, arguments: }, child: Text('Navigate to a named that accepts arguments'), ), ], ), ), ); } //ModalRoute accepts the parameter Class ExtractArgumentsScreen extends StatelessWidget {static const routeName = '/extractArguments'; @override Widget Build (BuildContext Context) {// modalRoute.of () returns the current route and its arguments final ScreenArguments arguments = ModalRoute.of(context).settings.arguments; return Scaffold( appBar: AppBar( title: Text(arguments.title), ), body: Center( child: Text(arguments.message), ), ); } // The constructor accepts the class PassArgumentsScreen extends StatelessWidget {static const routeName = '/passArguments'; final String title; final String message; PassArgumentsScreen({Key key, @required this.title, @required this.message}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text(title), ), body: Center( child: Text(message), ), ); }}Copy the code

Transition animation route Hero

import 'package:flutter/material.dart'; void main() => runApp(HeroAPP()); class HeroAPP extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build return MaterialApp( title: 'Transition Demo', home: MainScreen(), ); } } class MainScreen extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('Main Screen'), ), body: GestureDetector( onTap: () { Navigator.push(context, MaterialPageRoute(builder: (_) { return DetailScreen(); })); }, child: Hero( tag: 'imageHero', child: Image.network('https://picsum.photos/250?image=9'), )), ); } } class DetailScreen extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( body: GestureDetector( onTap: () { Navigator.pop(context); }, child: Center( child: Hero( tag: 'imageHero', child: Image.network('https://picsum.photos/250?image=9'), ), ), ), ); }}Copy the code