zoukankan      html  css  js  c++  java
  • [Flutter] Router Navigation

    Basic navigation by using 'Navigator.push' & 'Navigator.pop()', for example, we have two screen, screen1 and screen2, we want to navigate between two screens:

    // Screen1.dart
    
    import 'package:flutter/material.dart';
    import 'screen2.dart';
    
    class Screen1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.red,
            title: Text('Screen 1'),
          ),
          body: Center(
            child: RaisedButton(
              color: Colors.red,
              child: Text('Go Forwards To Screen 2'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) {
                    return Screen2();
                  }),
                );
              },
            ),
          ),
        );
      }
    }

    Screen2.dart:

    import 'package:flutter/material.dart';
    
    class Screen2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.blue,
            title: Text('Screen 2'),
          ),
          body: Center(
            child: RaisedButton(
              color: Colors.blue,
              child: Text('Go Back To Screen 1'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ),
        );
      }
    }

    Using the named route:

    In main.dart, we can list all the routes:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          // home: Screen0(),
          initialRoute: '/',
          routes: {
            '/': (context) => Screen0(),
            '/first': (context) => Screen1(),
            '/second': (context) => Screen2()
          },
        );
      }
    }

    To be noticed: 'home' & 'initialRoute' cannot be used at the same time.

           child: Column(
              children: <Widget>[
                RaisedButton(
                  color: Colors.red,
                  child: Text('Go To Screen 1'),
                  onPressed: () {
                    Navigator.pushNamed(context, '/first');
                  },
                ),
                RaisedButton(
                  color: Colors.blue,
                  child: Text('Go To Screen 2'),
                  onPressed: () {
                    Navigator.pushNamed(context, '/second');
                  },
                ),
              ],
            ),
  • 相关阅读:
    JVM的即时编译器JIT之简单介绍
    JS脚本动态给标签控件添加事件
    getParameterMap的使用
    IOS开发中判断文件是否存在,不存在则拷贝
    javaweb中解决Cookie中文乱码问题
    网页中的上标和下标实现
    Java中枚举的使用
    ASP.NET 首页性能的4大做法
    httpHandlers和httpModules接口介绍 (5)
    css+div排版如何支持所有浏览器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11668107.html
Copyright © 2011-2022 走看看