Mister disco
Mister disco

Reputation: 165

What is the significance of the settings variable in MaterialPageRoute?

I have a list of ListTile and whenever I tapped them, a new page will appear. Currently, the new page will slide up (when appearing) and slide down (when removed). I wanted to change the transition animation to Fade.

I've read the solution of that in here then I edited the code from the link and here is the result.

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);
    }
}

The only difference from the link's solution to mine was that I never supplied the "settings" variable to the MaterialPageRoute class.

And here is the part of the code where I've used the Navigator.push:

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

I've tried to run this code and I've never expected that this will run smoothly since I never provided the settings variable to the MaterialPageRoute widget but it ran perfectly.

My question is, is this the right way to do it? or Should I provide settings for the MaterialPageRoute class? And also since I didn't provide a settings variable for the MaterialPageRoute class, where did it get its settings? In this part of the code:

if (settings.isInitialRoute)
    return child;

I would appreciate any enlightment. Thanks in advance.

Upvotes: 2

Views: 1149

Answers (1)

rmtmckenzie
rmtmckenzie

Reputation: 40513

The settings are only meant for two things - specifying the name, and letting the route know whether it's the first page to be opened.

The isInitialRoute simply tells the route whether it's the first page to be opened; this is important because you don't want a slide up animation on the very first page.

Since it seems your Custom Route is really only used after the first page, you don't need to worry about this. So you're probably fine ignoring the settings, unless you start to use the page as your first page (and even then, fading in might not be the worst thing).

Upvotes: 2

Related Questions