Flutter ha due tipi fondamentali di widget: StatelessWidget (immutabile, senza stato interno che cambia) e StatefulWidget (può contenere e aggiornare stato mutabile, ricostruendosi quando lo stato cambia). Scegliere quello giusto è fondamentale per costruire UI Flutter.
StatelessWidget — nessuno stato che cambia
// a StatelessWidget: just describes UI based on its inputs (immutable, no internal state)
class Greeting extends StatelessWidget {
final String name;
const Greeting(this.name);
@override
Widget build(BuildContext context) {
return Text('Hello, $name'); // UI depends only on inputs, never changes itself
}
}
// → use for UI that doesn't change on its own (static content, displays based on inputs)
