flutter - 如何通过 Dart 中的 onTap 函数将值传递到另一个屏幕?
问题描述
我有两页。一是Route,二是Stops。此外,我的代码包含按路线对停靠点进行排序的算法。当我做测试示例并在同一页面上传递停靠点作为路由时,一切正常,但为了更好的 UI,我想将参数放在构造函数和 onTap 方法中。如何将此算法中的参数和另一个屏幕中的术语传递到另一个屏幕?
第一个屏幕:
body: FutureBuilder(
future: getMarshrutWithStops(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
List<RouteWithStops> routes = snapshot.data;
print(routes?.toString());
return (routes == null)
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StopsPage(
stId: routes[index].stop[index].stId
)));
},
//the algorithm which is sorted everything by id's
Future<List<RouteWithStops>> getMarshrutWithStops() async {
List<Routes> routes = [];
List<ScheduleVariants> variants = [];
List<StopList> stops = [];
final TransportService transService = TransportService();
routes.addAll((await transService.fetchroutes()).toList());
stops.addAll(await transService.fetchStops());
variants.addAll(await transService.fetchSchedule());
List<RouteWithStops> routesWithStops = [];
for (Routes route in routes) {
final routeWithStops = RouteWithStops();
routesWithStops.add(routeWithStops);
routeWithStops.route = route;
routeWithStops.variant =
variants.where((variant) => variant.mrId == route.mrId).first;
List<RaceCard> cards = [];
cards.addAll(
await transService.fetchRaceCard(routeWithStops.variant.mvId));
print(cards);
List<StopList> currentRouteStops = [];
cards.forEach((card) {
stops.forEach((stop) {
if (card.stId == stop.stId) {
currentRouteStops.add(stop);
}
});
});
routeWithStops.stop = currentRouteStops;
}
return routesWithStops;
}
我希望存储所有已排序站点的第二页:
class StopsPage extends StatelessWidget {
final int stId;
const StopsPage({Key key, this.stId}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: getMarshrutWithStops(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
List<RouteWithStops> routes = snapshot.data;
print(routes?.toString());
return (routes == null)
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: routes.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(routes[index].stop.toString()),
);
});
},
),
);
}
Future<List<RouteWithStops>> getMarshrutWithStops() async {
List<Routes> routes = [];
List<ScheduleVariants> variants = [];
List<StopList> stops = [];
final TransportService transService = TransportService();
routes.addAll((await transService.fetchroutes()).take(10).toList());
stops.addAll(await transService.fetchStops());
variants.addAll(await transService.fetchSchedule());
List<RouteWithStops> routesWithStops = [];
for (Routes route in routes) {
final routeWithStops = RouteWithStops();
routesWithStops.add(routeWithStops);
routeWithStops.route = route;
routeWithStops.variant =
variants.where((variant) => variant.mrId == route.mrId).first;
List<RaceCard> cards = [];
cards.addAll(
await transService.fetchRaceCard(routeWithStops.variant.mvId));
print(cards);
List<StopList> currentRouteStops = [];
cards.forEach((card) {
stops.forEach((stop) {
if (card.stId == stop.stId) {
currentRouteStops.add(stop);
}
});
});
routeWithStops.stop = currentRouteStops;
}
return routesWithStops;
}
}
我只是想我不需要把整个算法复制粘贴到所有页面上,也许我只需要以for循环开头的一部分算法并将其转移到第二页,所有过滤后的停止都应该是。我不知道在 onTap 函数中放入什么以及在 Stops 页面上将什么传递给构造函数。
解决方案
你可以做这样的事情
onTap
将First Page
值传递为name parameter
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => StopPage(stId: routes[index].stop[index].stId))
);
}
StopPage
通过使用构造函数,在 上超过相同的值
class StopPage extends StatefulWidget {
final dynamic stId;
StopPage({this.stId});
}
推荐阅读
- java - 如何在Java中停止响铃?
- r - plotly 和 ggplotly:隐藏和取消隐藏图形的部分
- django - 我是否需要从 django 的数据库中下载文件才能使用它?
- css - 下一步活动时如何更改角度步进器进度颜色
- reactjs - 反应拖放列表项
- javascript - 在函数内调用函数时 React 的范围问题
- android - 为什么即使创建了变量也没有初始化?
- android - 内部测试轨道的发布要求 - Google Play 商店
- excel - 使用Vlookup,但是数组单元格中有公式,怎么办?
- node.js - 在 NodeJS 应用程序中集成 SQL-Server