Home > Net >  Load a Route before pushing it via Navigator in flutter?
Load a Route before pushing it via Navigator in flutter?

Time:01-05

I'm currently trying to push a named route in flutter.

Works good so far, but the Asset Image for the background is loaded after the route was pushed via Navigator, which does not look good.

Currently I push the route like this:

  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 5), () =>
      Navigator.pushNamed(context, routeToPage2)
    );
  }

Is there any way to load a Page / Route without pushing it in the first place, so everything is build correctly when the route is pushed after the set time?

CodePudding user response:

I guess the Asset Image gets loaded on routeToPage2? In this case the push happens everytime before the image gets load.

CodePudding user response:

you can use a routegenerator for your page route and popUntil. This behavior will remove existing pages off the stack and push a single page called home page on. The MaterialPageRoute builds your route that is pushed to the navigator. Load your asset in the scaffold of HomePage. It should not require a delay to render

 Navigator.of(context)
                .popUntil(ModalRoute.withName(RouteGenerator.homePage));


class RouteGenerator {
  static const String homePage = "/home";
  static const String customPage = "/custom";
  RouteGenerator._();
  static Route<dynamic> handleRoute(RouteSettings routeSettings) {
    Widget childWidget;
    switch (routeSettings.name) {
      case homePage:
        {
          childWidget = HomePageWidget(title: 'Home Page');
        }
        break;
      case customPage:
        {
          final args = routeSettings.arguments as CustomView;
          childWidget = CustomPageWidget(args);
        }
        break;
      default:
        throw FormatException("Route Not Found");
    }
    return MaterialPageRoute(builder: (context) => childWidget);
  }
}
  •  Tags:  
  • Related