Skip to content

notifyListeners() doest not rebuild on adding new page #2

@vkmaxcooldude

Description

@vkmaxcooldude

notifyListeners() does not force rebuild the Navigator in my modified

import 'package:flutter/material.dart';
import 'package:demo_app/UI/auth/auth.dart';
import 'package:demo_app/UI/splashscreen.dart';

enum ParentRoutes { SplashScreen, Auth, Dashboard, Notification }

class ParentRouteConfig {
  const ParentRouteConfig(this.myRoute, [this.arguments]);
  final ParentRoutes myRoute;
  final Object arguments;
}

class ParentRouteInformationParser
    extends RouteInformationParser<ParentRouteConfig> {
  final Map<String, ParentRoutes> _routeStringToEnum = {
    "/": ParentRoutes.SplashScreen,
    "/auth": ParentRoutes.Auth,
    "/dashboard": ParentRoutes.Dashboard,
    "/notification": ParentRoutes.Notification,
  };
  @override
  Future<ParentRouteConfig> parseRouteInformation(
      RouteInformation routeInformation) async {
    final String routeName = routeInformation.location;
    // Change to Map
    final ParentRoutes route = _routeStringToEnum[routeName];
    if (route == null) throw Exception("Unknown Route");
    return ParentRouteConfig(route, routeInformation.state);
  }

  @override
  RouteInformation restoreRouteInformation(ParentRouteConfig configuration) {
    switch (configuration.myRoute) {
      case ParentRoutes.SplashScreen:
        return RouteInformation(location: '/', state: configuration);
      case ParentRoutes.Auth:
        return RouteInformation(location: '/auth', state: configuration);
      case ParentRoutes.Dashboard:
        return RouteInformation(location: '/dashboard', state: configuration);
      case ParentRoutes.Notification:
        return RouteInformation(
            location: '/notification', state: configuration);
    }
    throw 'unknown';
  }
}

class ParentRouterDelegate extends RouterDelegate<ParentRouteConfig>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<ParentRouteConfig> {
  @override
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  final List<Page<void>> _pages = <Page<void>>[
    MaterialPage(
        key: ValueKey("/"),
        name: "/",
        child: SplashScreen(),
        arguments: ParentRouteConfig(ParentRoutes.SplashScreen)),
  ];

  List<Page<void>> pages() => _pages;

  ParentRouteConfig _currentConfig;

  set currentConfiguration(ParentRouteConfig config) {
    if (config != null) _currentConfig = config;
    notifyListeners();
  }

  @override
  Future<void> setNewRoutePath(ParentRouteConfig configuration) async {
    currentConfiguration = configuration;
  }

  void addNewPage(ParentRouteConfig configuration) {
    if (configuration.myRoute == ParentRoutes.Auth) {
      currentConfiguration = configuration;
      _pages.add(
        MaterialPage(
            key: ValueKey("/auth"),
            name: "/auth",
            child:
                Authentication(currentScreen: (configuration.arguments as int)),
            arguments: configuration),
      );
    }
  }

  // For web application
  @override
  ParentRouteConfig get currentConfiguration => _currentConfig;

  bool _handlePopPage(Route<dynamic> route, dynamic result) {
    final bool success = route.didPop(result);
    if (success) {
      if (_pages.length > 1) {
        _pages.removeAt(_pages.length - 1);
        notifyListeners();
      } else {
        navigatorKey.currentState.pop();
      }
    }
    return success;
  }

  @override
  Widget build(BuildContext context) {
    return Navigator(
      key: navigatorKey,
      pages: pages(),
      onPopPage: _handlePopPage,
    );
  }
}```

I expected to simplify it and it should rebuild on `addNewPage(ParentRouteConfig configuration)`. The pages get added but the UI doesn't render until I use ValueKey Instead of NavigatorKey

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions