如何在 Flutter 中设置默认选中的单选按钮?
How to set a radio button checked by default in Flutter?
默认情况下,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",
),
],
),
解决方案:
要在 Flutter 中设置默认单选按钮,只需为单选按钮组值中使用的变量手动定义一个现有值。
String _radioValue = 'one'; //one| two | three - One by default
要将所有单选按钮保持为空,只需设置 var null:
String _radioValue = null; //all radio buttons blank
或最简单的方法:
String _radioValue; //all radio buttons blank
添加初始状态
class _ProductTypeScreen extends State<ProductType> {
String _radioValue; //Initial definition of radio button value
String choice;
// ------ [add the next block] ------
@override
void initState() {
setState(() {
_radioValue = "one";
});
super.initState();
}
// ------ end: [add the next block] ------
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
});
}
@override
Widget build(BuildContext context) {
我举一个简单的例子来理解:
int _radioSelected = 1;**
String _radioVal;
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Male'),
Radio(
value: 1,
groupValue: _radioSelected,
activeColor: Colors.blue,
onChanged: (value) {
setState(() {
_radioSelected = value;
_radioVal = 'male';
});
},
),
Text('Female'),
Radio(
value: 2,
groupValue: _radioSelected,
activeColor: Colors.pink,
onChanged: (value) {
setState(() {
_radioSelected = value;
_radioVal = 'female';
});
},
)
],
),
这就是我完成单选按钮的方式
int? _radioSelected;
String _radioVal = "";
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
Radio(
value: 1,
groupValue: _radioSelected,
activeColor: Colors.blue,
onChanged: (value) {
setState(() {
_radioSelected = value as int;
_radioVal = 'Check In';
print(_radioVal);
});
},
),
const Text("Check In"),
],
),
Row(
children: [
Radio(
value: 2,
groupValue: _radioSelected,
activeColor: Colors.red,
onChanged: (value) {
setState(() {
_radioSelected = value as int;
_radioVal = 'Check Out';
print(_radioVal);
});
},
),
const Text("Check Out"),
],
),
],
),
默认情况下,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",
),
],
),
解决方案:
要在 Flutter 中设置默认单选按钮,只需为单选按钮组值中使用的变量手动定义一个现有值。
String _radioValue = 'one'; //one| two | three - One by default
要将所有单选按钮保持为空,只需设置 var null:
String _radioValue = null; //all radio buttons blank
或最简单的方法:
String _radioValue; //all radio buttons blank
添加初始状态
class _ProductTypeScreen extends State<ProductType> {
String _radioValue; //Initial definition of radio button value
String choice;
// ------ [add the next block] ------
@override
void initState() {
setState(() {
_radioValue = "one";
});
super.initState();
}
// ------ end: [add the next block] ------
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
});
}
@override
Widget build(BuildContext context) {
我举一个简单的例子来理解:
int _radioSelected = 1;**
String _radioVal;
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Male'),
Radio(
value: 1,
groupValue: _radioSelected,
activeColor: Colors.blue,
onChanged: (value) {
setState(() {
_radioSelected = value;
_radioVal = 'male';
});
},
),
Text('Female'),
Radio(
value: 2,
groupValue: _radioSelected,
activeColor: Colors.pink,
onChanged: (value) {
setState(() {
_radioSelected = value;
_radioVal = 'female';
});
},
)
],
),
这就是我完成单选按钮的方式
int? _radioSelected;
String _radioVal = "";
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
Radio(
value: 1,
groupValue: _radioSelected,
activeColor: Colors.blue,
onChanged: (value) {
setState(() {
_radioSelected = value as int;
_radioVal = 'Check In';
print(_radioVal);
});
},
),
const Text("Check In"),
],
),
Row(
children: [
Radio(
value: 2,
groupValue: _radioSelected,
activeColor: Colors.red,
onChanged: (value) {
setState(() {
_radioSelected = value as int;
_radioVal = 'Check Out';
print(_radioVal);
});
},
),
const Text("Check Out"),
],
),
],
),