RadioListTile单选按钮组

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: [
          const SizedBox(width: 20),
          const Divider(),
          Column(
            children: [
              RadioListTile(
                  title: Text("男"),
                  value: 1,
                  groupValue: sex,
                  onChanged: _radioChange),
              RadioListTile(
                  title: Text("女"),
                  value: 2,
                  groupValue: sex,
                  onChanged: _radioChange),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[Text("$sex"), Text(sex == 1 ? "男" : "女")],
              ),
            ],
          )
        ],
      ),
    );
  }
}

 

posted on 2024-01-31 17:50  鲤斌  阅读(14)  评论(0)    收藏  举报