Junior — Middle
What user interface components have you worked with and changed their state?
sobes.tech AI
Answer from AI
In Flutter, you often work with user interface components such as TextField, Checkbox, Switch, Slider, DropdownButton, and buttons (ElevatedButton, TextButton). To change their state, StatefulWidget and setState() methods are usually used.
For example, for a Switch toggle, you can store a boolean value and update it upon user interaction:
class MySwitchWidget extends StatefulWidget {
@override
_MySwitchWidgetState createState() => _MySwitchWidgetState();
}
class _MySwitchWidgetState extends State<MySwitchWidget> {
bool isSwitched = false;
@override
Widget build(BuildContext context) {
return Switch(
value: isSwitched,
onChanged: (value) {
setState(() {
isSwitched = value;
});
},
);
}
}
For more complex state management, patterns and libraries such as Provider, Bloc, Riverpod are used.