How to update a page content with Navigator 2.0

Viewed 87

I'm using Navigator 2.0 in Flutter and I have pages that have a type and a code to identify themselves.

I have a Drawer composed of Listile, which can go to pages of different type and code or go to pages of the same type but different code. So my problem is navigate between pages of the same type and different code

This is my stack of pages into the delegate:

  List<Page> get _loggedInStack {
    final onLogout = () async {
      await auth.logout();
      splash = true;
      loggedIn = false;
      _clear();
    };
    return [
  if (selectedPageType == "dashboard")
    DashboardPage(
        onPageChange: (String page, String code) {
          selectedPageType = page;
          selectedPageCode = code;
          print(selectedPageCode);
        },
        id: selectedPageCode,
        onLogout: onLogout),
   if (selectedPageType == "chart")
        ChartPage(
            id: selectedPageCode,
            onPageChange: (String page, String code) {
              selectedPageType = page;
              selectedPageCode = code;
              print(selectedPageCode);
            },
            onLogout: onLogout),
       ...
    ];
  }

Here is where I go to a selected page:

  goToSelectedPage(String type, String config) {
    switch (type) {
      case ('Monitoring'):
        widget.onPageChange('dashboard', config);
        break;
      case ('Charts'):
        widget.onPageChange('chart', config);
        break;
      
      ...
    }
  }

Here an example of a Page:

class DashboardPage extends Page {
  DashboardPage(
      {required this.onPageChange, required this.onLogout, required this.id})
      : super(key: ValueKey(AppStrings.DASHBOARD_PAGE_KEY));

  final Function(String, String) onPageChange;
  final VoidCallback onLogout;
  final String id;

  @override
  Route createRoute(BuildContext context) {
    return PageRouteBuilder(
        transitionDuration: Duration(seconds: 0),
        settings: this,
        pageBuilder: (context, animation1, animation2) {
          return DashboardScreen(
            onPageChange: onPageChange,
            onLogout: onLogout,
            id: id,
            key: ValueKey(AppStrings.DASHBOARD_SCREEN_KEY),
          );
        });
  }
}

this is what happen

So, when I try to change from a page that has the same selectedPageType but not the same selectedPageCode nothing happen

1 Answers

welcome to the community!

The problem would seem that in calling the same selectedPageType with different selectedPageCode you are using the same key (AppStrings.DASHBOARD_SCREEN_KEY). So, try to change the key for each time you call your DashboardPage and your DashboardScreen, for example adding the selectedPageCode :

key: ValueKey(AppStrings.DASHBOARD_SCREEN_KEY + selectedPageCode)

By doing this, you shouldn't have any more problems!

Related