Flutter Navigator跳转到下一个页面并返回(页面之
2019-07-25 本文已影响0人
charmingcheng
创建两个Flutter页面,FirstScreen与SecondScreen
使用 Navigator.push()方法导航到下一界面
使用 Navigator.pop()方法返回到上一个界面
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstScreen(),
));
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: RaisedButton(
child: Text('Launch screen'),
onPressed: () {
Navigator.push( //跳转到第二个界面
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);//从第二个界面返回
},
child: Text('Go back!'),
),
),
);
}
}