Skip to content

Instantly share code, notes, and snippets.

@ulusoyca
Last active January 23, 2023 18:37
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/a65bd18add3f76035b2202e21d2af8a3 to your computer and use it in GitHub Desktop.
Save ulusoyca/a65bd18add3f76035b2202e21d2af8a3 to your computer and use it in GitHub Desktop.
Router Delegate 03
class MyAppRouterDelegate extends RouterDelegate with ChangeNotifier, PopNavigatorRouterDelegateMixin {
final GlobalKey<NavigatorState> _navigatorKey;
final AuthRepository authRepository;
final ColorsRepository colorsRepository;
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();
}
List<Color> _colors;
List<Color> get colors => _colors;
set colors(List<Color> value) {
_colors = value;
notifyListeners();
}
@override
GlobalKey<NavigatorState> get navigatorKey => _navigatorKey;
MyAppRouterDelegate(this.authRepository, this.colorsRepository) : _navigatorKey = GlobalKey<NavigatorState>() {
_init();
}
_init() async {
loggedIn = await authRepository.isUserLoggedIn();
if (loggedIn) {
colors = await colorsRepository.fetchColors();
}
}
@override
Widget build(BuildContext context) {
List<Page> stack;
if (loggedIn == null || (loggedIn && colors == 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 {
String process;
if (loggedIn == null) {
process = 'Checking login state...';
} else if (colors == null) {
process = 'Fetching colors...';
}
return [
SplashPage(process: process),
];
}
List<Page> get _loggedOutStack =>
[
LoginPage(onLogin: () async {
loggedIn = true;
colors = await colorsRepository.fetchColors();
})
];
List<Page> get _loggedInStack {
final onLogout = () async {
loggedIn = false;
_clear();
};
return [
HomePage(
onColorTap: (String colorCode) {
selectedColorCode = colorCode;
},
colors: colors,
onLogout: onLogout,
),
if (selectedColorCode != null)
ColorPage(
selectedColorCode: selectedColorCode,
onShapeTap: (ShapeBorderType shape) {
this.selectedShapeBorderType = shape;
},
onLogout: onLogout,
),
if (selectedShapeBorderType != null)
ShapePage(
colorCode: selectedColorCode,
shapeBorderType: selectedShapeBorderType,
onLogout: onLogout,
)
];
}
_clear() {
selectedColorCode = null;
selectedShapeBorderType = null;
colors = null;
}
Future<void> setNewRoutePath(configuration) async { /* Do Nothing */ }
}
@eximius313
Copy link

Is loggedIn && necessary in if (loggedIn == null || (loggedIn && colors == null))?

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