Last active
January 23, 2023 18:36
-
-
Save ulusoyca/c10ad320085f10690f74655d81434a68 to your computer and use it in GitHub Desktop.
nav2_router_delegate_02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using Provider, why do you have:
inside
MyAppRouterDelegate
instead of using Provider for creating separateAppState
and injecting this AppState using:
so in
MyAppRouterDelegate
you could simply do:?
(of course this requires you to pass
AppState
to theHomePage
,ColorPage
, andShapePage
accordingly)