Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vkmaxcooldude opened this issue Feb 26, 2021 · 1 comment
Open

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

vkmaxcooldude opened this issue Feb 26, 2021 · 1 comment

Comments

@vkmaxcooldude
Copy link

vkmaxcooldude commented Feb 26, 2021

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
@chunhtai
Copy link
Owner

This looks like a bug in framework, can you file a bug in flutter/flutter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants