【问题标题】:Flutter Page View how to not always start at the initialPage?Flutter Page View 如何不总是从 initialPage 开始?
【发布时间】:2021-06-26 06:53:12
【问题描述】:

这听起来可能很复杂,但我试着解释一下我想要做什么

我有一个包含不同页面的页面视图以及另一个页面视图,如下所示:

PageView(
  controller: pageController,
  physics: NeverScrollableScrollPhysics(),
  children: [FirstPage(), 
             SecondPage(), 
             SmallPageView(), 
             ThirdPage()])

虽然按预期工作,但存在一个问题:SmallPageView() 有 4 页。当我浏览它的页面时,我在ThirdPage() 并按下返回按钮,它调用:

pageController.previousPage(duration: Duration(milliseconds: 400), curve: Curves.ease);

我又在第 1 页的SmallPageView()。这是有道理的,因为它是pageControllerinitialPagepreviousPage。但是,当我在 ThirdPage() 并按后退按钮时,我想转到 SmallPageView()last 页面,这显然更直观

我尝试做类似的事情

pageController.previousPage(duration: Duration(milliseconds: 400), curve: Curves.ease);
smallPageViewControllerHouse.jumpToPage(4);

这不起作用,我想看起来会很丑。

我也无法将变量分配给控制器的 initialPage 以动态更改它。有没有办法“记住”页面视图的位置,而不是每次都从初始页面开始?

当然,您可以争辩说只解决 SmallPageView() 并将其 4 个页面放入 PageView() 而不是嵌套页面视图。但这是一个设计解决方案,要实现我想要的外观,除了使用嵌套页面视图之外别无他法(据我所知 atm)

【问题讨论】:

标签: android flutter user-interface


【解决方案1】:

尝试使用IndexedStack,indexedstack里面的pageview会保持它的状态

IndexedStack(
  index: _currentIndex,
  children: [FirstPage(), 
             SecondPage(), 
             SmallPageView(), 
             ThirdPage()])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多