Radio单选按钮组

class RadioPage extends StatefulWidget {
  const RadioPage({super.key});
  @override
  State<RadioPage> createState() => _RadioPageState();
}

class _RadioPageState extends State<RadioPage> {
  int sex = 1;
  _radioChange(value) {
    setState(() {
      sex = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Radio"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text("男:"),
              Radio(
                value: 1,
                onChanged: _radioChange,
                groupValue: sex,
              ),
              const SizedBox(width: 20),
              const Text("女:"),
              Radio(
                value: 2,
                onChanged: _radioChange,
                groupValue: sex,
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [Text("$sex"), Text(sex == 1 ? "男" : "女")],
          ),
        ],
      ),
    );
  }
}

 

posted on 2024-01-19 19:03  鲤斌  阅读(9)  评论(0)    收藏  举报