天天看點

Navigator

flutter 的頁面跳轉

import 'package:flutter/material.dart';
import 'package:flutter_demo2/SecondScreen.dart';
void main(){
  runApp(MaterialApp(
    title: '第一個導航頁面',
    home: MyApp(),
  ));
}



class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text('第一個頁面')),
      body: Container(
        child: RaisedButton(
          child: Text('跳轉到第二個頁面'),
            onPressed: (){
            Navigator.push(context, MaterialPageRoute(
                builder: (context) => new SecondScreen(),
            ));
            }),
      ),
    );
  }

}


           
import 'package:flutter/material.dart';

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text('第二個頁面')),
      body: Center(

        child: RaisedButton(
            child: Text('傳回'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ),
    );
  }
}