flutter路由Navigator.pushNamed传参觉得有⽤点个赞
Routers.dart 这个⽂件⽤于配置路由
import 'package:flutter/material.dart';
import 'package:flutterapp/searchPage.dart';
import 'package:flutterapp/tabs.dart';
import 'form.dart';
//配置路由
final routes ={
"/":(context,{arguments})=>Tabs(),
"/form":(context,{arguments})=>formPage(title: arguments),
"/search":(context)=>searchPage()
};
//固定写法
var onGenerateRoute =(RouteSettings settings){
final String name = settings.name;
final Function pageContentBuilder = routes[name];
if(pageContentBuilder != null){
if(settings.arguments != null){
final Route route =MaterialPageRoute(
builder:(context)=>
pageContentBuilder(context, arguments: settings.arguments));
return route;
}else{
final Route route =
MaterialPageRoute(builder:(context)=>pageContentBuilder(context));
flutter开发appreturn route;
}
}
};
main.dart
void main()=>runApp(My_APP());
class My_APP extends StatelessWidget {
@override
Widget build(BuildContext context){
// TODO: implement build
return MaterialApp(
home:Scaffold(
body:Tabs(),
),
theme:ThemeData(primarySwatch: llow),
initialRoute:'/',//初始化加载路由
onGenerateRoute: onGenerateRoute,
);
}
}
这个是个布局页⾯
Tabs.dart
import 'category.dart';
import 'home.dart';
class Tabs extends StatefulWidget {
@override
State<StatefulWidget>createState(){
// TODO: implement createState
return_TabState();
}
}
class _TabState extends State<Tabs>{
int _currentIndex =0;
List _pageList=[
homePage(),
categoryPage(),
settingPage()
];
@override
Widget build(BuildContext context){
// TODO: implement build
return Scaffold(
appBar:AppBar(
title:Text("Flutter Demo"),
),
body:this._pageList[this._currentIndex],
bottomNavigationBar:BottomNavigationBar(
currentIndex:this._currentIndex,
onTap:(int index){
setState((){
this._currentIndex = index;
});
},
//        iconSize: 50,
fixedColor: an,//选中时候的颜⾊
type: BottomNavigationBarType.fixed,//配置地步tabs可以有多个按钮⾃适应        items:[
BottomNavigationBarItem(icon:Icon(Icons.home), title:Text("⾸页")),
BottomNavigationBarItem(icon:Icon(Icons.category), title:Text("分类")),
BottomNavigationBarItem(icon:Icon(Icons.settings), title:Text("设置")),
],
),
);
}
}
从category.dart 跳转到 form.dart
category.dart
class categoryPage extends StatefulWidget {
@override
State<StatefulWidget>createState(){
// TODO: implement createState
return_categoryPageState();
}
}
class _categoryPageState extends State<categoryPage>{  @override
Widget build(BuildContext context){
// TODO: implement build
return Column(
crossAxisAlignment: ,
mainAxisAlignment: ,
children:<Widget>[
RaisedButton(
child:Text("跳转到表单页⾯"),
onPressed:(){
Navigator.pushNamed(context,"/form",arguments:{ "title":"123"//参数map
});
})
],
);
}
}
form.dart
class formPage extends StatelessWidget {
final title;
formPage({this.title});//⽤构造函数接受参数map
@override
Widget build(BuildContext context){
/
/ TODO: implement build
return Scaffold(
appBar:AppBar(
title:Text(title["title"]),//取map中对应key的值),
body:ListView(
children:<Widget>[
ListTile(
title:Text("我是表单页⾯"),
),
ListTile(
title:
Text("我是表单页⾯"),
),
ListTile(
title:Text("我是表单页⾯"),
),
],
),
floatingActionButton:FloatingActionButton(
child:Text("返回"),
onPressed:(){
Navigator.of(context).pop();
}),
);
}
}
这样就可以通过传参⾃定义form的标题了
亲测可⽤
route可以都在第⼀个⽂件⾥⾯配置,⾮常好⽤有问题可以评论区留⾔

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。