Flutter学习八:Navigator页面跳转练习

在Flutter中页面跳转使用的是Navigator和RactNative思想一样代码如下

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

/**
 * 跳转到新页面并返回
 */
void main() {
  //application
  runApp(new MaterialApp(
    //application名字
    title: "FlutterApplication",
    //页面
    home: new FirstPage(),
  ));
}

/**
 * 第一个页面
 */
class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("第一个页面"),
      ),
      body: new Center(
        child: new RaisedButton(
            child: new Text("点我进入第二个页面"),
            onPressed: () {
              //跳转到新的 页面我们需要调用 navigator.push方法 这个和eactNative的方式相同
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => new SecondPage()));
            }),
      ),
    );
  }
}

/**
 * 第二个页面
 */
class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("第二个页面"),
      ),
      body: new Center(
        //onPressed  点击事件
        child: new RaisedButton(
            child: new Text("点我回第一个页面"),
            onPressed: () {
              //回到上一个页面 相当于finsh
              Navigator.pop(context);
            }),
      ),
    );
  }
}

第一个页面图:

第二个页面图:

猜你喜欢

转载自blog.csdn.net/sinat_29256651/article/details/81394626