I am confused between these three ways of passing/building widgets:
1.
Widget _myDisplay() {
return [widgets showing content];
}
@override
Widget build(BuildContext context) {
return _myDisplay();
}
@override
Widget build(BuildContext context) {
return MyDisplay();
}
where MyDisplay
is defined as such (I'm not sure if it's crucial whether MyDisplay
is a StatelessWidget
or a StatefulWidget
):
class MyDisplay extends StatelessWidget {
const MyDisplay({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return [widgets showing content];
}
}
Widget _myDisplay = [widgets showing content];
@override
Widget build(BuildContext context) {
return _myDisplay;
}
I've read this thread comparing the first two methods, and from what I understand, using a unique, named class extending StatelessWidget
or StatefulWidget
allows you to use the const
keyword which signifies that it will not be rebuilt when the Widget tree is rebuilt.
However, what about the 3rd method above? Is it the same as 1. or 2., or completely different? If so, how is it different and when is it preferred?
Thanks!