Creating immutable widgets
There are two kinds of visual widget in Flutter: stateless and stateful.
A stateless widget is simple, lightweight, and performant. Flutter can render hundreds of stateless widgets without breaking a sweat.
Stateless widgets are immutable. Once they are created, they cannot be modified. Flutter only has to concern itself with these widgets once. It doesn’t have to maintain any complex life cycle states or worry about a block of code modifying them. In fact, the only way to modify a stateless widget is by deleting it and creating a new one.
How to do it...
Start off by creating a brand-new Flutter project called flutter_layout
, either via your IDE or the command line. Don’t worry about the sample code generated. We’re going to delete it and start from scratch:
- Open
main.dart
and delete everything. - Then, type the following code into the editor:
void main() => runApp(const...