Flutter Stateless Widgets
If you look a user interface, it consists of many Widgets but not many of them have to be smart or interact with the user.
If you look at the default flutter application, there are several widgets but only in fact one Widget with any interactions with the user – the ‘MyHomePage’ Widget that has a counter that counts up when the user clicks on the floating button.
So, the rest of the widgets are used to display something, not interact with the user. That is what stateless widgets are for.
Minimum Code
Here is the minimum code you need for a Stateless Widget:class EmptyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return <Insert Some Widgets Here>;
}
}
Creation
Stateless widgets are created by a parent widget in its ‘build’ method. They are given the information they need to do their job when they are created.Stateless widgets receive arguments (information) from their parent widget in the ‘build’ method, which they store in final member variables.
Example
CarWidget("Bmw", "M3","https://media.ed.edmundsmedia.com/bmw/m3/2018/oem/2018_bmw_m3_sedan_base_fq_oem_4_150.jpg"),
‘Bmw’
Stored in member variable ‘make’.
‘M3’
Stored in member variable ‘model’.
"https://media.ed.edmundsmedia.com/bmw/m3/2018/oem/2018_bmw_m3_sedan_base_fq_oe
m_4_150.jpg’
Stored in member variable ‘imageSrc’.