Skip to content

Instantly share code, notes, and snippets.

@ulusoyca
Last active January 23, 2023 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulusoyca/c10ad320085f10690f74655d81434a68 to your computer and use it in GitHub Desktop.
Save ulusoyca/c10ad320085f10690f74655d81434a68 to your computer and use it in GitHub Desktop.
nav2_router_delegate_02
class MyAppRouterDelegate extends RouterDelegate with ChangeNotifier, PopNavigatorRouterDelegateMixin {
final GlobalKey<NavigatorState> _navigatorKey;
bool _loggedIn;
bool get loggedIn => _loggedIn;
set loggedIn(value) {
_loggedIn = value;
notifyListeners();
}
String _selectedColorCode;
String get selectedColorCode => _selectedColorCode;
set selectedColorCode(String value) {
_selectedColorCode = value;
notifyListeners();
}
ShapeBorderType _selectedShapeBorderType;
ShapeBorderType get selectedShapeBorderType => _selectedShapeBorderType;
set selectedShapeBorderType(ShapeBorderType value) {
_selectedShapeBorderType = value;
notifyListeners();
}
final AuthRepository authRepository;
@override
GlobalKey<NavigatorState> get navigatorKey => _navigatorKey;
MyAppRouterDelegate(this.authRepository) : _navigatorKey = GlobalKey<NavigatorState>() {
_init();
}
_init() async {
loggedIn = await authRepository.isUserLoggedIn();
}
@override
Widget build(BuildContext context) {
List<Page> stack;
if (loggedIn == null) {
stack = _splashStack;
} else if (loggedIn) {
stack = _loggedInStack;
} else {
stack = _loggedOutStack;
}
return Navigator(
key: navigatorKey,
pages: stack,
onPopPage: (route, result) {
if (!route.didPop(result)) return false;
if (selectedShapeBorderType == null) selectedColorCode = null;
selectedShapeBorderType = null;
return true;
},
);
}
List<Page> get _splashStack => [SplashPage(process: 'Splash Screen:\n\nChecking auth state')];
List<Page> get _loggedOutStack => [
LoginPage(onLogin: () {
loggedIn = true;
})
];
List<Page> get _loggedInStack {
final onLogout = () {
loggedIn = false;
_clear();
};
return [
HomePage(
onColorTap: (String colorCode) {
selectedColorCode = colorCode;
},
onLogout: onLogout,
),
if (selectedColorCode != null)
ColorPage(
selectedColorCode: selectedColorCode,
onShapeTap: (ShapeBorderType shapeBorderType) {
this.selectedShapeBorderType = shapeBorderType;
},
onLogout: onLogout,
),
if (selectedShapeBorderType != null)
ShapePage(
colorCode: selectedColorCode,
shapeBorderType: selectedShapeBorderType,
onLogout: onLogout,
)
];
}
_clear() {
selectedColorCode = null;
selectedShapeBorderType = null;
}
@override
Future<void> setNewRoutePath(configuration) async { /* Do Nothing */ }
}
@eximius313
Copy link

eximius313 commented Jan 6, 2023

If you are using Provider, why do you have:

  bool _loggedIn;
  bool get loggedIn => _loggedIn;
  set loggedIn(value) {
    _loggedIn = value;
    notifyListeners();
  }

  String _selectedColorCode;
  String get selectedColorCode => _selectedColorCode;
  set selectedColorCode(String value) {
    _selectedColorCode = value;
    notifyListeners();
  }

  ShapeBorderType _selectedShapeBorderType;
  ShapeBorderType get selectedShapeBorderType => _selectedShapeBorderType;
  set selectedShapeBorderType(ShapeBorderType value) {
    _selectedShapeBorderType = value;
    notifyListeners();
  }

inside MyAppRouterDelegate instead of using Provider for creating separate AppState

class AppState extends ChangeNotifier {
  bool _loggedIn;
  bool get loggedIn => _loggedIn;
  set loggedIn(value) {
    _loggedIn = value;
    notifyListeners();
  }

  String _selectedColorCode;
  String get selectedColorCode => _selectedColorCode;
  set selectedColorCode(String value) {
    _selectedColorCode = value;
    notifyListeners();
  }

  ShapeBorderType _selectedShapeBorderType;
  ShapeBorderType get selectedShapeBorderType => _selectedShapeBorderType;
  set selectedShapeBorderType(ShapeBorderType value) {
    _selectedShapeBorderType = value;
    notifyListeners();
  }
}

and injecting this AppState using:

MultiProvider(
      providers: [
        ChangeNotifierProvider<AuthViewModel>(create: (_) => AuthViewModel(authRepository)),
        ChangeNotifierProvider<AppState>(create: (_) => AppState()),
      ],
      child: Builder(
        builder: (context) => MaterialApp(home: Router(routerDelegate: delegate)),)
      ),
    )

so in MyAppRouterDelegate you could simply do:

  @override
  Widget build(final BuildContext context) {
    final AppState appState = context.watch<AppState>();
    List<Page> stack;
    if (appState.loggedIn == null) {
      stack = _splashStack;
    } else if (appState.loggedIn) {
      stack = _loggedInStack;
    } else {
      stack = _loggedOutStack;
    }
    return Navigator(
      key: navigatorKey,
      pages: stack,
      onPopPage: (route, result) {
        if (!route.didPop(result)) return false;
        if (selectedShapeBorderType == null) selectedColorCode = null;
        selectedShapeBorderType = null;
        return true;
      },
    );
  }

?
(of course this requires you to pass AppState to the HomePage, ColorPage, and ShapePage accordingly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment