參考: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: "多窗口切換標(biāo)題",
home: Scaffold(
appBar: AppBar(
title: Text("命名路由練習(xí)"),
),
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("命名路由跳轉(zhuǎn)"),
onPressed: (){
//頁面跳轉(zhuǎn) 在這里字節(jié)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);
//返回頁面?zhèn)鬟f的參數(shù)
// Navigator.of(context).pop("這是返回攜帶的參數(shù)");
}
),
),
);
}
}