获取上一个页面pop的返回参数 Navigator.pop(context, {'title': '123789'});
在跳转的地方
onTap: () async{
var result = await Navigator.pushNamed(context, '/avatar');
print(result);
},
修改完成后, 最好重启项目 flutter run
route.dart里面
import 'package:flutter/material.dart';
import '../home.dart';
import '../other/searchPage.dart';
////配置路由
var routes = {
'/':(context) => Home(),
'/search':(context, { arguments }) => SearchPage(arguments: arguments),
};
//固定写法
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));
return route;
}
}
};
main.dart里面
import 'package:flutter/material.dart';
import 'app/routers/route.dart';;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
initialRoute: '/',
// home: Home(),
onGenerateRoute: onGenerateRoute
);
}
}
发送传参的地方
Navigator.pushNamed(context, '/search', arguments: {
'title': '13213'
});
接收的地方 无状态的组件接收 StatelessWidget
class SearchPage extends StatelessWidget{
var arguments; // 要接收别的组件传过来的值 只能是arguments
SearchPage({arguments});
@override
Widget build(BuildContext context) {
// TODO: implement buildr
return Container(
child: Text('12321321大大${arguments != null ? arguments["title"] : ""}'),
);
}
}
接收的地方 有状态的组件接收 StatefulWidget
class ProductInfoPage extends StatefulWidget {
final Map arguments;
ProductInfoPage({Key key,arguments}) : super(key: key);
_ProductInfoPageState createState() => _ProductInfoPageState(arguments: arguments);
}
class _ProductInfoPageState extends State<ProductInfoPage> {
Map arguments;
_ProductInfoPageState({arguments});
@override
Widget build(BuildContext context) {
return null;
}
}