I have implemented GoRouter in the flutter app and managed all the routes and the app flow and navigation works fine. However when I open the keyboard for any input fields like text field and form field then the app redirects to the top of the stack. I have managed the authentication so if am logged out opening keyboard rebuilds and am back to login page and if I am logged in then am again back to my homescreen. What may have caused this issue?
I have tried wrapping the material app with consumer to listen to the change but still the issue persists.
here is my code:
Widget build(BuildContext context) {
return ChangeNotifierProvider<AuthService>(
create: (_) => authService,
child: Consumer<AuthService>(
builder: (context, authService, _) {
return MaterialApp.router(
routerConfig:router(authService),
);
},
static GoRouter router(AuthService authService) => GoRouter(
initialLocation: authService.isAuthenticated ? RouteNames.dashboard : RouteNames.login,
refreshListenable: authService,
routes: [
GoRoute(
path: '/',
builder: (context, state) => authService.isAuthenticated ? const DashboardScreen() : const LoginScreen(),
),
GoRoute(
path: RouteNames.login,
builder: (context, state) => const LoginScreen(),
),
GoRoute(
path: RouteNames.register,
builder: (context, state) => const RegisterScreen(),
),
GoRoute(
path: RouteNames.passwordReset,
builder: (context, state) => const pwdRstScreen(),
),
GoRoute(
path: RouteNames.dashboard,
builder: (context, state) => const DashboardScreen(),
routes: [
GoRoute(
path: RouteNames.firstPage.lastPathSegment(),
builder: (context, state) {
return FirstPage(
title: title,
path: path,
);
},
routes:[
//here are my nested routes]
),
],
),
],
redirect: (context, state) {
final loggedIn = authService.isAuthenticated;
final loggingIn = state.subloc == RouteNames.login;
if (!loggedIn && !loggingIn) return RouteNames.login;
if (loggedIn && loggingIn) return RouteNames.dashboard;
return null;
},
);
}
//singleton authservice class:
class AuthService extends ChangeNotifier {
static final AuthService _instance = AuthService._internal();
factory AuthService() {
return _instance;
}
AuthService._internal() {
_loadFromPrefs();
}
bool _isAuthenticated = prefs.isAuthenticated();
bool get isAuthenticated => _isAuthenticated;
void _loadFromPrefs() async {
_isAuthenticated = CorePrefs.isAuthenticated();
notifyListeners();
}
Future<void> setAuthenticated(bool value) async {
if (_isAuthenticated != value) {
_isAuthenticated = value;
await prefs.isAuthenticated(value: value);
notifyListeners();
}
}
}
return ChangeNotifierProvider(
create: (context) => AuthService(),
child: MyApp(),
);
//inside MyApp
//widget build...
final AuthService authService = AuthService();
return Consumer<AuthService>(
builder: (context, authService, child) {
return MaterialApp.router(...);}