Flutter开发之——交互组件-Radio和RadioListTile

本文详细介绍了Flutter中的两种单选按钮组件:Radio和RadioListTile。Radio组件包含基本构造方法、属性及使用示例,而RadioListTile提供了额外的标题和子标题功能,同时详细解释了其属性和用法。通过实例代码展示了如何创建和控制多个单选按钮的选择状态。
摘要由CSDN通过智能技术生成

一 概述

本文介绍Flutter中的单选按钮组件

  • Radio:只有选中按钮的组件
  • RadioListTile:既有按钮也有按钮标题的组件

二 Radio

2.1 构造方法

const Radio({
    Key? key,
    required this.value,
    required this.groupValue,
    required this.onChanged,
    this.mouseCursor,
    this.toggleable = false,
    this.activeColor,
    this.fillColor,
    this.focusColor,
    this.hoverColor,
    this.overlayColor,
    this.splashRadius,
    this.materialTapTargetSize,
    this.visualDensity,
    this.focusNode,
    this.autofocus = false,
  })

2.2 常用属性

属性说明取值
value按钮被选中的值T
groupValue一组按钮中被选中的值T
activeColor选中颜色Colors
onChanged选中变化回调final ValueChanged<T?>?

2.3 示例(一个Radio)

代码
var _radioValue = "1";
var _groupValue = "";
Radio(
       value: _radioValue,
       activeColor: Colors.red,
       groupValue: _groupValue,
       onChanged: (value) {
             setState(() {
               _groupValue = value;
             });
    })
效果图

2.4 示例(多个Radio)

代码
 var _radioGroupValue = '选项1';
Row(
     mainAxisAlignment: MainAxisAlignment.center,
     children: [
              	Radio(
                      value: "选项1",
                      groupValue: _radioGroupValue,
                      onChanged: (value) {
                        setState(() {
                          _radioGroupValue = value;
                        });
                      }),
                  Radio(
                      value: "选项2",
                      groupValue: _radioGroupValue,
                      onChanged: (value) {
                        setState(() {
                          _radioGroupValue = value;
                        });
                      }),
                  Radio(
                      value: "选项3",
                      groupValue: _radioGroupValue,
                      onChanged: (value) {
                        setState(() {
                          _radioGroupValue = value;
                        });
                      })
                ],
  )
效果图

三 RadioListTile

3.1 构造方法

  const RadioListTile({
    Key? key,
    required this.value,
    required this.groupValue,
    required this.onChanged,
    this.toggleable = false,
    this.activeColor,
    this.title,
    this.subtitle,
    this.isThreeLine = false,
    this.dense,
    this.secondary,
    this.selected = false,
    this.controlAffinity = ListTileControlAffinity.platform,
    this.autofocus = false,
    this.contentPadding,
    this.shape,
    this.tileColor,
    this.selectedTileColor,
  })

3.2 常用属性

属性说明取值
value按钮被选中的值T
groupValue一组按钮中被选中的值T
activeColor选中颜色Colors
onChanged选中变化回调final ValueChanged<T?>?
title按钮标题Widget
subtitle按钮子标题Widget
controlAffinity①文本放置控件的位置ListTileControlAffinity枚举
controlAffinity①
取值说明
leading勾选框在开头位置
trailing勾选框在结尾位置
platform根据平台确定

3.3 示例

代码
var _radioGroupValue = '选项1';
Row(
     	children: [
              Flexible(
                      child: RadioListTile(
                          title: Text("选项1", style: TextStyle(fontSize: 12),),
                          value: "选项1",
                          onChanged: (value) {
                            _radioGroupValue = value;
                          },
                          groupValue: _radioGroupValue,
                          subtitle: Text("subTitle", style: TextStyle(fontSize: 10)),
                          controlAffinity: ListTileControlAffinity.platform)),
                  Flexible(
                      child: RadioListTile(
                          title: Text("选项2", style: TextStyle(fontSize: 12)),
                          value: "选项2",
                          groupValue: _radioGroupValue,
                          onChanged: (value) {
                            _radioGroupValue = value;
                          },
                          subtitle: Text("subTitle", style: TextStyle(fontSize: 10)),
                          controlAffinity: ListTileControlAffinity.platform)),
                  Flexible(
                      child: RadioListTile(
                          title: Text("选项3", style: TextStyle(fontSize: 12)),
                          value: "选项3",
                          subtitle: Text("subTitle", style: TextStyle(fontSize: 10)),
                          groupValue: _radioGroupValue,
                          onChanged: (value) {
                            _radioGroupValue = value;
                          },
                          controlAffinity: ListTileControlAffinity.platform))
                ],
)
效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值