Wouter Pleijsier
Wouter Pleijsier

Reputation: 23

Keeping a route alive in Flutter

I have a flutter app where can see several cards. These cards are clickable, and will show a InAppWebView with a custom made viewer, which exists on the website I link to. The loading of this InAppWebView is pretty taxing on the app however, and I would like to pre-load this as much as possible.

It is kinda hard to give a lot of details, since this is job-related and thus private information, so I can't be too specific. I am also the only one working with Flutter here, so no advice can be expected from colleagues.

I am hoping to reduce the loading times of the InAppWebView by having it opened in the background already, and then just swapping to the correct route instead of initializing it from scratch. The problem here is that the URL will change, but that can be handled inside the view instead of creating an entirely new InAppWebView.

In short: How do I create a widget/route in the background of my Flutter application, and swap to it when necessary?

Upvotes: 2

Views: 1205

Answers (2)

Fernando Herrera
Fernando Herrera

Reputation: 679

Use IndexedStack for that.

Create a List of widgets you want to swap (and keep its state)

 final viewRoutes = const <Widget>[
    Screen1(),
    Screen2(),
    Screen3(),
 ];

Then use the IndexedStack to swap them

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
         index: 0, // <--- change this depending on the page
      ),
    );
  }

Upvotes: 0

Kantine
Kantine

Reputation: 771

Maybe you can use a Stack and a Visibility widgets ? like this:

bool _isWebPageVisible = false;

Stack(
  children: <Widget>[
    MainWidget(),
    Visibility(
      visible: _isWebPageVisible,
      child: WebPageWidget()
  ]
)

And you could change _isWebPageVisible whenever needed. When visible, the WebPageWidget will appear above the MainWidget.

Upvotes: -1

Related Questions