1、普通路由
routes: routes,
onTap: () {
Navigator.of(context).pushNamed('taskDetail',
arguments:listViewData[index]);
},
Text(ModalRoute.of(context).settings.arguments.toString();)
2、路由拦截onGenerateRoute
我想把传过来的参数,放在initState中,初始化一个变量,再以变量为参数发送请求获得数据
void initState() {
super.initState();
_taskTrunk = ModalRoute.of(context).settings.arguments;
}
查了下网上的同学们提供的方法onGenerateRoute
initialRoute: '/',
onGenerateRoute: onGenerateRoute,
import 'package:AGV/view/login_view.dart';
import 'package:AGV/view/mainPage_view.dart';
import 'package:AGV/view/task/taskDetail_view.dart';
import 'package:flutter/material.dart';
import 'package:AGV/view/task/taskAdd_view.dart';
final routes = {
"/": (context, {arguments}) => MainPageView(),
"login": (context, {arguments}) => LoginView(),
"taskAdd": (context, {arguments}) => TaskAddView(),
"taskDetail": (context, {arguments}) => TaskDetailView(arguments: arguments),
};
var onGenerateRoute = (RouteSettings settings) {
final Function pageBuilder = routes[settings.name];
print('********************************');
if (pageBuilder != null) {
if (settings.arguments != null) {
final Route route = MaterialPageRoute(
builder: (context) {
return pageBuilder(context, arguments: settings.arguments);
},
settings: settings,
);
return route;
} else {
return MaterialPageRoute(builder: (context) => pageBuilder(context));
}
}
return MaterialPageRoute(builder: (context) => MainPageView());
};
onTap: () {
Navigator.of(context).pushNamed('taskDetail',
arguments: {'taskData': listViewData[index]});
},
class TaskDetailView extends StatefulWidget {
TaskDetailView({Key key, this.arguments}) : super(key: key);
final Map arguments;
@override
_TaskDetailViewState createState() => _TaskDetailViewState();
}
class _TaskDetailViewState extends State<TaskDetailView> {
TaskTrunk _taskTrunk;
void initState() {
super.initState();
_taskTrunk = widget.arguments['taskData'];
}
|