Flutter Rendering
The ‘Build’ Method
Stateless Widgets generate their UI in their ‘build’ method, theresult of which is rendered by Flutter.
They can build their UI using values from their member variables, or from other sources.
They cannot force themselves to re-render.
Values from Member Variables
When a Stateless Widget is asked to build a UI, it can use the values from these member variables to render the UI (probably with other Stateless Widget children). These values don’t change, they are set in the constructor and that’s it.Example
The code below builds a UI to display info about a car using the information from the member variables.@override
Widget build(BuildContext context) {
return Center(
child: Column(children: <Widget>[
Text(make),
Text(model),
Image.network(imageSrc)
]));
}
Values from Other Sources
When a Stateless Widget is asked to build a UI, it can use values from other sources, for example InheritedWidgets (which can store information).Example
The code below builds a UI to say “Hi There”, using information from another source (the ‘Theme’ inherited widget) to determine text color.@override
Widget build(BuildContext context) {
return Center(
child: Column(children: <Widget>[
Text("Hello", style: Theme.of(context).textTheme.display1),
Text("There", style: Theme.of(context).textTheme.display1)
]));
}