如何通过 Dart 中的 onTap 函数将值传递到另一个屏幕?

Posted

技术标签:

【中文标题】如何通过 Dart 中的 onTap 函数将值传递到另一个屏幕?【英文标题】:How to pass values to the another screen by the onTap function in Dart? 【发布时间】:2021-10-28 23:57:33 【问题描述】:

我有两页。一是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 页面上将什么传递给构造函数。

【问题讨论】:

参考这个***.com/a/68259836/2804581 试试this回答 【参考方案1】:

你可以这样做

First PageonTap 中,将值传递为 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);

【讨论】:

以上是关于如何通过 Dart 中的 onTap 函数将值传递到另一个屏幕?的主要内容,如果未能解决你的问题,请参考以下文章

如何将任意数据从 Dart 聚合物 Web 组件传递给单击事件处理函数

如何在 DART 中重新加载 api 调用?

通过R中的变量将值传递给函数

如何将值传递给 CloudWatch 中的 Lambda 函数?

如何将值从 javascript 函数传递到 django 视图

将值传递给多个函数