【问题标题】:Does flutter calls child screen if we navigate back(using back button in app bar) from a child screen to parent screen?如果我们从子屏幕导航到父屏幕(使用应用栏中的后退按钮),颤动会调用子屏幕吗?
【发布时间】:2023-03-11 06:49:01
【问题描述】:

该应用程序具有以下屏幕,

  1. 选项卡屏幕(主/主屏幕)
  2. CategoriesScreen(第一个选项卡屏幕)
  3. 最喜欢的屏幕(第二个标签屏幕)
  4. CategoryMealsScreen(CategoryScreen 的子屏幕)

我已经为每个屏幕构建方法添加了打印语句来检查导航,

当我从“CategoryMealsScreen”导航回“CategoriesScreen”时,我可以看到“CategoryMealsScreen”再次被调用,为什么会这样?

日志:

I/flutter (30320): TabsScreen  ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): CategoryMealsScreen ( child screen, pressed back in this screen)

I/flutter (30320): CategoryMealsScreen ( child screen, once again it's being called after landing in CategoriesScreen)

I/flutter (30320): TabsScreen  ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): CategoryMealsScreen ( child screen)

I/flutter (30320): CategoryMealsScreen ( child screen, once again it's being called after landing in CategoriesScreen)

I/flutter (30320): TabsScreen ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): TabsScreen

I/flutter (30320): FavoritesScreen

I/flutter (30320): TabsScreen ( parent screen )

I/flutter (30320): CategoriesScreen 

I/flutter (30320): CategoryMealsScreen  ( child screen)

I/flutter (30320): CategoryMealsScreen ( i pressed back button in-app bar and navigated back to parent  screen but once again child screen was printed in logs )

下面是从 CategoriesScreen 推送 CategoryMealsScreen 的代码,

Navigator.of(ctx).pushNamed(
      CategoryMealsScreen.routeName,
      arguments: {
        'id': id,
        'title': title,
      },
    );

我的期望是当我导航回“TabsScreen”时不会再次调用“CategoryMealsScreen”

提前致谢。

【问题讨论】:

    标签: flutter dart routes


    【解决方案1】:

    Flutter 中的“导航返回”是使用navigation.pop() 完成的。这可以追溯到导航历史中的一步。如果你想返回几个步骤,你可以使用这个:

    Navigator.of(context).popUntil((route) => route.isFirst);
    

    这将导航回您的小部件树的根目录(根据您的描述,我认为这就是您想要的)。

    正在重建的小部件似乎是颤振的预期行为,请参阅:https://github.com/flutter/flutter/issues/11655

    以下是关于处理不需要的重建的一些想法:https://stackoverflow.com/a/52249579/2102328

    【讨论】:

    • 嗨 Schnodderbalken,感谢您的回答,但我的问题是,我只向后导航一个屏幕,但是当颤动正在加载历史记录中的前一个屏幕时,正在调用弹出构建方法的屏幕又为什么会这样?
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多