【问题标题】:flutter navigator router without onpressed没有onpressed的颤振导航器路由器
【发布时间】:2022-01-24 06:00:58
【问题描述】:

我的项目中有一个路由器类,我想在其中使用路由器中的地址。我知道我应该使用导航器。 push(context, my router) 但我想在没有 onpressed 或 onTap 的情况下使用它。我想返回小部件。 这是我的班级路由器:

Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
  '/firstPage': (context) => const FirstPage(),
  
  '/': (context) => const BottomNavBar( activeTabIndex: 0),
  '/exploreHome/podcastsProfile': (context) =>PodcastsProfile(podId: exploreManager.podId),
  '/exploreHome/morepagepod': (context) =>MorePod(moreId: exploreManager.moreId,showProfilePodcasts: exploreManager.showProfilePodcasts),
  '/exploreHome/morepageepd': (context) =>MoreEpd(moreId: exploreManager.moreId ),
};

我想使用路由器类而不是直接调用我的类:

使用'/exploreHome/podcastsProfile' 而不是PodcastsProfile(podId: podId);

 Widget renderBody(String page) {
    Widget widget = Container();
    switch (page) {
      case podcastsProfile:
        widget = **PodcastsProfile(
          podId: podId,
        );**
    }
    return Container();
  }

【问题讨论】:

    标签: flutter dart widget router navigator


    【解决方案1】:

    大多数人使用WidgetsBinding.instance.addPostFrameCallback 作为一种黑客手段,在帧渲染后触发路由或任何命令式代码。

    https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html

    也就是说,这是一个 hack,我建议您重新考虑一下您是如何到达这里的。字符串page 是如何成为构建方法的一部分的?当page 状态发生变化而不是在构建方法中时,你能做出反应吗?我的两分钱。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 2022-11-25
      • 2018-10-11
      • 2022-12-11
      • 2022-10-18
      • 1970-01-01
      • 2020-07-14
      • 2021-05-19
      • 1970-01-01
      相关资源
      最近更新 更多