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');
                  },
                ),
              ],
            ),
  • 相关阅读:
    P2604 [ZJOI2010]网络扩容
    P2053 [SCOI2007]修车
    P2045 方格取数加强版
    P4134 [BJOI2012]连连看
    P2153 [SDOI2009]晨跑
    P3381 【模板】最小费用最大流
    P3376 【模板】网络最大流
    P1326 足球
    2020牛客多校第八场I题 Interesting Computer Game(并查集+判环)
    Codeforces 1375D Replace by MEX(思维题)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11668107.html
Copyright © 2011-2022 走看看