介绍Radio的用法
① 当value的值和groupValue的值一致时,按钮就会被选中。
Radio(
value: true,
groupValue: true,
onChanged: (bool? value){
}),
Radio(
value: true,
groupValue: true,
onChanged: (bool? value){
}),
② 两者二选一
bool isApproved = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('title'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pushed this button many times'),
Text('test2'),
Radio(
value: true,
groupValue: isApproved,
activeColor: Colors.red,
onChanged: (Object? value){
if(value == null){
return;
}
value as bool;
isApproved = value;
setState((){});
}),
Radio(
value: false,
groupValue: isApproved,
activeColor: Colors.red,
onChanged: (Object? value){
if(value == null){
return;
}
value as bool;
isApproved = value;
setState((){});
}),
],
),
),
);
}