首页 > 建站教程 > APP开发,混合APP >  Flutter笔记43:Radio单选组件正文

Flutter笔记43:Radio单选组件

Flutter的Radio单选组件非常简单,下面是简单的代码:
class _DemoPageState extends State<DemoPage> {
  int groupValue = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        //次轴居中
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Radio(
            //选中颜色
            activeColor: Colors.red,
            
            //值
            value: 1,
            //当value与groupValue一致时,显示选中状态
            groupValue: groupValue,
            onChanged: (T) {
              this.setState(() {
                groupValue = T;
              });
            }),
          Radio(
            value: 2,
            groupValue: groupValue,
            onChanged: (T) {
              this.setState(() {
                groupValue = T;
              });
            }),
          Radio(
            value: 3,
            groupValue: groupValue,
            onChanged: (T) {
              this.setState(() {
                groupValue = T;
              });
            })
        ],
      ),
    );
  }
}