Have a look at the following code snippet.
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final child = CustomTextWidget();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: RaisedButton(
child: child, //CustomTextWidget(),
onPressed: () {
setState(() {});
}
),
),
);
}
}
class CustomTextWidget extends StatelessWidget {
CustomTextWidget() {
print("constructed");
}
@override
Widget build(BuildContext context) {
print("got built");
return Text("click me");
}
}
I can understand why in the following code the constructor and the build method of CustomTextWidget will be called with every build. It's because I'm giving a new object every time.
RaisedButton(
child: CustomTextWidget(),
onPressed: () {
setState(() {});
}
),
but why is it that if I give it child the build method of the CustomTextWidget object does not get called with every build? The constructor is called one time which makes sense and then the build method just a single time. Why isn't build called with every setState?
RaisedButton(
child: child,
onPressed: () {
setState(() {});
}
),
How does flutter decide when to call these build methods?