A state is the data present in a graphical object at a certain moment
A state is the data present in a graphical object at a certain moment
In this case, we need an ephemeral, aka local, state. This kind of state represents just a single Widget's data.
class MyWidget extends StatefulWidget {
//...
State<MyWidget> createState() => _MyWidget();
}
class _MyWidget extends State<MyWidget>{
int myState;
void initState() {
super.initState();
myState = 0;
}
void myMethod() {
setState(() { myState = 1; })
}
}
In flutter, controllers allow to give control to the parent widget over its child's state.
In flutter, controllers allow to give control to the parent widget over its child's state.
If the state is our data, the controller is what manages it.
class _MyCustomFormState extends State<MyCustomForm> {
final myController = TextEditingController(text: "some default text");
@override
void initState() {
super.initState();
myController.addListener(_printLatestValue);
}
}
TextField(
controller: myController,
),
void _printLatestValue() {
final text = myController.text;
print('Second text field: $text (${text.characters.length})');
}