Dominik Roszkowski
Dominik Roszkowski

Reputation: 2563

How to remove pages from stack using Navigator.pages in Flutter

I'm trying to understand Navigator 2.0 Pages API which allows to modify navigation stack via Navigator.pages property.

I prepared simple demo where I try to remove pages from the backstack by updating the provided list of pages.

Here is the DartPad demo

screen recording of the issue

However, when simply removing the CustomPage from the Navigator.pages there's an animation glitch. For a split second you can see the page being animated beneath the current page.

Is it a bug in Flutter framework or is there a better way to remove pages from the backstack?

Upvotes: 2

Views: 1038

Answers (1)

Ali Qanbari
Ali Qanbari

Reputation: 3121

Adding a ValueKey to the Navigator widget seems to solve the problem:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Navigator Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Navigator(
        key: ValueKey(pages.length), // <------------- 
        pages: List.unmodifiable(pages),
        onPopPage: _onPopPage,
      ),
    );
  }

Upvotes: 1

Related Questions