参考:http://www.jspkongjian.com/news.jsp?id=1496
main.dart文件
import 'package:flutter/material.dart';
import 'home.dart';
void main() {
runApp(NaviPushApp());
}
//push
class NaviPushApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: "多窗口切换标题",
home: Scaffold(
appBar: AppBar(
title: Text("命名路由练习"),
),
body: MyRoute(),
),
//在MaterialApp里面有个routes的方法,在这里定义命名路由
// /test 是自定义路由命名,context是上下文, HomePage是下一页的类名,注意需要引入这个头文件路径
routes: {
'/test':(context)=>HomePage(),
},
);
}
}
class MyRoute extends StatelessWidget {
// const MyRoute({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: FloatingActionButton(
child: Text("命名路由跳转"),
onPressed: (){
//页面跳转 在这里字节pushName(上下文,命名路由名称即可)
Navigator.pushNamed(context,'/test');
},
),
);
}
}
home.dart文件
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'hometitle',
home: Scaffold(
appBar: AppBar(title: Text('home page title'),),
body: Container(
// alignment: TextAlign.right,
child: Text('first home page'),
),
floatingActionButton: FloatingActionButton(
child: Text('back'),
onPressed: (){
//返回
Navigator.pop(context);
//返回页面传递的参数
// Navigator.of(context).pop("这是返回携带的参数");
}
),
),
);
}
}