Transfer pages without animation | Navigator.of(context).pushNamed()

Issue

I’m using flutter for the web.
The page is transitioning with Navigator.of(context).pushNamed(Home.route);, but the animation is played at this time.
Is it possible to disable this animation?

I wrote the following code in the main() function of main.dart.

MaterialApp(
initialRoute: '/home',
routes: {
  '/home': (context) => Home(),
  '/profile': (context) => Profile(),
},

@immutable
class Home extends StatelessWidget {
  static const String route = '/home';
  ....
}

@immutable
class Profile extends StatelessWidget {
  static const String route = '/profile';
....
}

And as I mentioned earlier, the following code is used to transition the page.

 Navigator.of(context).pushNamed(Home.route);

I was able to transition pages without animation by using Navigator.push, but I want to use Navigator.of(context).pushNamed() because the link does not change.

Does anyone know how to disable animation while still using Navigator.of(context).pushNamed()?
Thank you.

Solution

I solved this problem.

I didn’t use Navigator.of().pushNamed() but, it is possible to show URL of pages.

first, I removed routes: from MaterialApp() and added onGenerateRoute: like this;

onGenerateRoute: (settings) {
      Widget wid = Home();
      switch (settings.name) {
        case "/home":
          wid = Home();
          break;
        case "/profile":
          wid = Profile();
          break;
      }

      if (wid != null) {
        return PageRouteBuilder(
            settings:
                settings, // Pass this to make popUntil(), pushNamedAndRemoveUntil(), works
            pageBuilder: (_, __, ___) => wid,
            transitionsBuilder: (_, a, __, c) =>
                FadeTransition(opacity: a, child: c));
      }
      // Unknown route
      return MaterialPageRoute(builder: (_) => Home());
    },

And when you want to move page, just call this;

Navigator.of(context).pushReplacementNamed('/home');

Answered By – Beginner_

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *