There are multiple reasons :
-
Widgets are immutable. Since
StatefulWidgetextendsWidgetit therefore must be immutable too. Splitting the declaration into two classes allows bothStatefulWidgetto be immutable andStateto be mutable. -
Widgets are instantiated using the syntax
new MyWidget(). If we merged both classes into one,new MyWidget()would reset all the properties of the state every time its parent update.
As for the explanation of class _MyStatefulState extends State<MyStateful>
That is because the State class can access to it’s Stateful part using the this.widget field.
The generic is here to make that field of type MyStateful instead of just StatefulWidget. As you may want to access MyStateful properties.