По умолчанию Flutter показывает все переключатели пустыми (не отмечены).
Как установить переключатель по умолчанию?
Я отправляю этот вопрос, чтобы задокументировать мое решение, чтоможет помочь кому-то, а также начать тему об этом, потому что я ничего не нашел об этом здесь.
Ниже кода переключателя:
class _ProductTypeScreen extends State<ProductType> {
String _radioValue; //Initial definition of radio button value
String choice;
void radioButtonChanges(String value) {
setState(() {
_radioValue = value;
switch (value) {
case 'one':
choice = value;
break;
case 'two':
choice = value;
break;
case 'three':
choice = value;
break;
default:
choice = null;
}
debugPrint(choice); //Debug the choice in console
});
}
// Now in the BuildContext... body widget:
@override
Widget build(BuildContext context) {
//First of the three radio buttons
Row(
children: <Widget>[
Radio(
value: 'one',
groupValue: _radioValue,
onChanged: radioButtonChanges,
),
Text(
"One selected",
),
],
),