【问题标题】:What is the significance of the settings variable in MaterialPageRoute?MaterialPageRoute中设置变量的意义是什么?
【发布时间】:2018-09-05 14:38:17
【问题描述】:

我有一个 ListTile 列表,每当我点击它们时,都会出现一个新页面。目前,新页面将向上滑动(出现时)和向下滑动(移除时)。我想将过渡动画更改为 Fade。

我已经阅读了here 中的解决方案,然后我编辑了链接中的代码,结果如下。

class MyCustomRoute<T> extends MaterialPageRoute<T> {
    MyCustomRoute({ WidgetBuilder builder})
        : super(builder: builder);

    @override
    Widget buildTransitions(BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child) {

        if (settings.isInitialRoute)
            return child;

        return new FadeTransition(opacity: animation, child: child);
    }
}

与我的链接解决方案的唯一区别是我从未将“设置”变量提供给 MaterialPageRoute 类。

这是我使用 Navigator.push 的代码部分:

new ListTile(
    onTap: (){
        Navigator.push(context, new MyCustomRoute(builder: (context) => new SecondPage("someTitle", "someDescription") ));
    },
    //The rest of the code goes here

我尝试运行此代码,但我从未想过它会顺利运行,因为我从未向 MaterialPageRoute 小部件提供设置变量,但它运行完美。

我的问题是,这是正确的做法吗?要么 我应该为 MaterialPageRoute 类提供设置吗? 而且由于我没有为 MaterialPageRoute 类提供设置变量,它的设置是从哪里获得的?在这部分代码中:

if (settings.isInitialRoute)
    return child;

我将不胜感激。提前致谢。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    这些设置仅用于两件事 - 指定名称,并让路由知道它是否是要打开的第一个页面。

    isInitialRoute 只是告诉路由它是否是要打开的第一页;这很重要,因为您不希望在第一页上显示上滑动画。

    由于您的自定义路线似乎真的只在第一页之后使用,您不必担心这一点。所以你可能忽略这些设置也没问题,除非你开始使用该页面作为你的第一页(即使那样,淡入可能不是最糟糕的事情)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2012-10-20
      • 2018-01-07
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多