I have created an example project that resembles Navigate to a new screen and back example. The only difference that I have TextField
with autofocus: true
on every screen:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstScreen(),
));
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("FirstScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("SecondScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ThirdScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}
class ThirdScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("ThirdScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FourthScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}
class FourthScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("FourthScreen build");
return Scaffold(
body: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}
When I navigate from ThirdScreen
to FourthScreen
my log looks like this:
I/flutter ( 7523): FourthScreen build
I/flutter ( 7523): SecondScreen build
I/flutter ( 7523): ThirdScreen build
I/flutter ( 7523): FourthScreen build
I/flutter ( 7523): SecondScreen build
I/flutter ( 7523): ThirdScreen build
I/flutter ( 7523): FourthScreen build
- Why after FourthScreen is built at the first time SecondScreen, ThirdScreen and FourthScreen build is called again?
- Why screens in the backstack are being reguild in the keyboard pops out?
- Why FirstScreen is not being rebuild?