TextField click rebuilds/reloads widget after routed
up vote
2
down vote
favorite
I am having an issue where the whole screen widget reloads when there is a textfield is used.
This doesn't happen when the the app is loaded with this screen as landing page.
But when the routing happens from another page to this page and when textfield is clicked then the rebuild happens.
I even tried a simple app and this is getting reproduced. Tried many ways but could not get to a solution.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 1"), // screen title
),
body: new Center(
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () {
button1(context);
},
child: new Text("Go to Screen 2"),
)
],
),
),
);
}
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("Widget rebuilds");
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 2"),
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
height: 350.0,
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 16.0, color: Colors.black),
)),
],
),
),
);
}
}
void main() {
runApp(new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
}
void button1(BuildContext context) {
print("Button 1");
Navigator.of(context).pushNamed('/screen2');
}
Here the app is loaded with screen 1 and clicking on button Go to screen2 will load the screen 2 with the text field. Clicking on this field will bring the keyboard and clicking the done on keyboard and again focusing on the text field will rebuild the screen. This keeps happening when keyboard appears and when disappears.
But then if Screen2 is set as landing page, then clicking on the text field and doing the same process mentioned above will not reload the widget. The widget build happens only once. Seems the issue is when the Screen2 is navigated from Screen 1
runApp(new MaterialApp(
home: new Screen2(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
add a comment |
up vote
2
down vote
favorite
I am having an issue where the whole screen widget reloads when there is a textfield is used.
This doesn't happen when the the app is loaded with this screen as landing page.
But when the routing happens from another page to this page and when textfield is clicked then the rebuild happens.
I even tried a simple app and this is getting reproduced. Tried many ways but could not get to a solution.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 1"), // screen title
),
body: new Center(
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () {
button1(context);
},
child: new Text("Go to Screen 2"),
)
],
),
),
);
}
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("Widget rebuilds");
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 2"),
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
height: 350.0,
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 16.0, color: Colors.black),
)),
],
),
),
);
}
}
void main() {
runApp(new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
}
void button1(BuildContext context) {
print("Button 1");
Navigator.of(context).pushNamed('/screen2');
}
Here the app is loaded with screen 1 and clicking on button Go to screen2 will load the screen 2 with the text field. Clicking on this field will bring the keyboard and clicking the done on keyboard and again focusing on the text field will rebuild the screen. This keeps happening when keyboard appears and when disappears.
But then if Screen2 is set as landing page, then clicking on the text field and doing the same process mentioned above will not reload the widget. The widget build happens only once. Seems the issue is when the Screen2 is navigated from Screen 1
runApp(new MaterialApp(
home: new Screen2(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
why did you importcupertinoas well?
– stt106
Nov 22 at 9:07
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am having an issue where the whole screen widget reloads when there is a textfield is used.
This doesn't happen when the the app is loaded with this screen as landing page.
But when the routing happens from another page to this page and when textfield is clicked then the rebuild happens.
I even tried a simple app and this is getting reproduced. Tried many ways but could not get to a solution.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 1"), // screen title
),
body: new Center(
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () {
button1(context);
},
child: new Text("Go to Screen 2"),
)
],
),
),
);
}
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("Widget rebuilds");
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 2"),
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
height: 350.0,
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 16.0, color: Colors.black),
)),
],
),
),
);
}
}
void main() {
runApp(new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
}
void button1(BuildContext context) {
print("Button 1");
Navigator.of(context).pushNamed('/screen2');
}
Here the app is loaded with screen 1 and clicking on button Go to screen2 will load the screen 2 with the text field. Clicking on this field will bring the keyboard and clicking the done on keyboard and again focusing on the text field will rebuild the screen. This keeps happening when keyboard appears and when disappears.
But then if Screen2 is set as landing page, then clicking on the text field and doing the same process mentioned above will not reload the widget. The widget build happens only once. Seems the issue is when the Screen2 is navigated from Screen 1
runApp(new MaterialApp(
home: new Screen2(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
I am having an issue where the whole screen widget reloads when there is a textfield is used.
This doesn't happen when the the app is loaded with this screen as landing page.
But when the routing happens from another page to this page and when textfield is clicked then the rebuild happens.
I even tried a simple app and this is getting reproduced. Tried many ways but could not get to a solution.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 1"), // screen title
),
body: new Center(
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () {
button1(context);
},
child: new Text("Go to Screen 2"),
)
],
),
),
);
}
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("Widget rebuilds");
return new Scaffold(
appBar: new AppBar(
title: new Text("Screen 2"),
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
height: 350.0,
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 16.0, color: Colors.black),
)),
],
),
),
);
}
}
void main() {
runApp(new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
}
void button1(BuildContext context) {
print("Button 1");
Navigator.of(context).pushNamed('/screen2');
}
Here the app is loaded with screen 1 and clicking on button Go to screen2 will load the screen 2 with the text field. Clicking on this field will bring the keyboard and clicking the done on keyboard and again focusing on the text field will rebuild the screen. This keeps happening when keyboard appears and when disappears.
But then if Screen2 is set as landing page, then clicking on the text field and doing the same process mentioned above will not reload the widget. The widget build happens only once. Seems the issue is when the Screen2 is navigated from Screen 1
runApp(new MaterialApp(
home: new Screen2(),
routes: <String, WidgetBuilder>{
'/screen2': (BuildContext context) => new Screen2()
},
));
edited Nov 22 at 8:35
Aniruddh Parihar
2,16111027
2,16111027
asked Nov 22 at 8:27
rahulmr
3161214
3161214
why did you importcupertinoas well?
– stt106
Nov 22 at 9:07
add a comment |
why did you importcupertinoas well?
– stt106
Nov 22 at 9:07
why did you import
cupertino as well?– stt106
Nov 22 at 9:07
why did you import
cupertino as well?– stt106
Nov 22 at 9:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
This is the normal behavior, there is no problem with it. It is in fact within the specs of build method :
It can be called an arbitrary number of time and you should expect it to be so.
If this causes a problem to do so, it is very likely that your build function is not pure. Meaning that it contains side effects such as http calls or similar.
These should not be done within the build method. More details here : How to deal with unwanted widget build?
About the "What triggers the build", there are a few common situations:
- Route pop/push, for in/out animations
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)pattern) change
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets likeTextFormFieldwithin theinitStatemethod otherwise they are being rebuilt every time the widget'sbuildmethod is called.
– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besidesprint. But he likely has some in his application or else it wouldn't cause an issue
– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
This is the normal behavior, there is no problem with it. It is in fact within the specs of build method :
It can be called an arbitrary number of time and you should expect it to be so.
If this causes a problem to do so, it is very likely that your build function is not pure. Meaning that it contains side effects such as http calls or similar.
These should not be done within the build method. More details here : How to deal with unwanted widget build?
About the "What triggers the build", there are a few common situations:
- Route pop/push, for in/out animations
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)pattern) change
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets likeTextFormFieldwithin theinitStatemethod otherwise they are being rebuilt every time the widget'sbuildmethod is called.
– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besidesprint. But he likely has some in his application or else it wouldn't cause an issue
– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
|
show 1 more comment
up vote
2
down vote
This is the normal behavior, there is no problem with it. It is in fact within the specs of build method :
It can be called an arbitrary number of time and you should expect it to be so.
If this causes a problem to do so, it is very likely that your build function is not pure. Meaning that it contains side effects such as http calls or similar.
These should not be done within the build method. More details here : How to deal with unwanted widget build?
About the "What triggers the build", there are a few common situations:
- Route pop/push, for in/out animations
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)pattern) change
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets likeTextFormFieldwithin theinitStatemethod otherwise they are being rebuilt every time the widget'sbuildmethod is called.
– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besidesprint. But he likely has some in his application or else it wouldn't cause an issue
– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
This is the normal behavior, there is no problem with it. It is in fact within the specs of build method :
It can be called an arbitrary number of time and you should expect it to be so.
If this causes a problem to do so, it is very likely that your build function is not pure. Meaning that it contains side effects such as http calls or similar.
These should not be done within the build method. More details here : How to deal with unwanted widget build?
About the "What triggers the build", there are a few common situations:
- Route pop/push, for in/out animations
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)pattern) change
This is the normal behavior, there is no problem with it. It is in fact within the specs of build method :
It can be called an arbitrary number of time and you should expect it to be so.
If this causes a problem to do so, it is very likely that your build function is not pure. Meaning that it contains side effects such as http calls or similar.
These should not be done within the build method. More details here : How to deal with unwanted widget build?
About the "What triggers the build", there are a few common situations:
- Route pop/push, for in/out animations
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)pattern) change
edited Nov 22 at 9:37
answered Nov 22 at 9:06
Rémi Rousselet
22.5k23875
22.5k23875
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets likeTextFormFieldwithin theinitStatemethod otherwise they are being rebuilt every time the widget'sbuildmethod is called.
– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besidesprint. But he likely has some in his application or else it wouldn't cause an issue
– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
|
show 1 more comment
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets likeTextFormFieldwithin theinitStatemethod otherwise they are being rebuilt every time the widget'sbuildmethod is called.
– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besidesprint. But he likely has some in his application or else it wouldn't cause an issue
– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
What's the side effect in this case?
– stt106
Nov 22 at 9:08
What's the side effect in this case?
– stt106
Nov 22 at 9:08
You basically want to initialise any interactive widgets like
TextFormField within the initState method otherwise they are being rebuilt every time the widget's build method is called.– SnakeyHips
Nov 22 at 9:15
You basically want to initialise any interactive widgets like
TextFormField within the initState method otherwise they are being rebuilt every time the widget's build method is called.– SnakeyHips
Nov 22 at 9:15
His example doesn't have any side-effect besides
print. But he likely has some in his application or else it wouldn't cause an issue– Rémi Rousselet
Nov 22 at 9:28
His example doesn't have any side-effect besides
print. But he likely has some in his application or else it wouldn't cause an issue– Rémi Rousselet
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
He said he can reproduce the problem with the sample code posted.
– stt106
Nov 22 at 9:28
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
Yeah, he reproduces the fact that the widget tree rebuilds. It happens on route change for animation purpose or on keyboard appear due to screen resize
– Rémi Rousselet
Nov 22 at 9:31
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426678%2ftextfield-click-rebuilds-reloads-widget-after-routed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why did you import
cupertinoas well?– stt106
Nov 22 at 9:07