首页 > 解决方案 > 如何通过 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 页面上将什么传递给构造函数。

标签: flutterdart

解决方案


你可以做这样的事情

onTapFirst 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});
}

推荐阅读