I'm using flutter navigation as follows:
return MaterialApp(
title: 'Title',
onGenerateRoute: _getRoute,
);
Route _getRoute(RouteSettings settings) {
if (settings.name == '/') {
return MaterialPageRoute(
settings: settings,
builder: (context) {
print('route / build'); //<---------------check
return LoginPage();
});
} else if (settings.name == '/someother') {
return MaterialPageRoute(
settings: settings,
builder: (context) {
return SomeOtherPage();
});
}
return null;
}
When app starts the LoginPage opens as expected and the check comment 'route / build'
prints to console. The LoginPage has two TextFields (username,password), when I focus on username field, the 'route / build'
prints again which means the LoginPage is rebuilt, when I fill username and go to next field (password) the page is rebuilt again and username value is lost.
Why the page is being rebuilt and how to solve it?
UPDATE: The real problem was not because of rebuilds but because of FocusNode
in LoginPage
which was StatelessWidget
so FocusNode
lifecycle was not managed. That caused text to dissapear and two focuses appear simultaneously on both TextFields
on page rebuild.