【Flutter】八、Flutter之表单开关——Switch、SwitchListTile

本文详细介绍了Flutter中用于表单的两种开关控件:Switch和SwitchListTile。Switch用于简单的开关操作,而SwitchListTile则提供了一个带有标题的开关选项。文章深入解析了这两种控件的构造器参数、属性说明及示例代码,帮助开发者更好地理解和使用这些控件。
部署运行你感兴趣的模型镜像

Flutter中开关控件有:Switch、SwitchListTile。Switch仅仅可以定义一个开关,SwitchListTile是一个有标题的开关。

一、Switch

1.1 Switch构造器

const Switch({
    Key key,
    @required this.value,
    @required this.onChanged,
    this.activeColor,
    this.activeTrackColor,
    this.inactiveThumbColor,
    this.inactiveTrackColor,
    this.activeThumbImage,
    this.inactiveThumbImage,
    this.materialTapTargetSize,
    this.dragStartBehavior = DragStartBehavior.start,
  }) : _switchType = _SwitchType.material,
       assert(dragStartBehavior != null),
       super(key: key);

1.2 Switch属性说明

属性说明
bool value开关的值
ValueChanged onChanged开关状态改变回调
Color activeColor激活状态的滑块颜色
Color activeTrackColor激活状态的轨道颜色
Color inactiveThumbColor关闭状态的滑块颜色
Color inactiveTrackColor关闭状态的轨道颜色
ImageProvider activeThumbImage激活状态滑块显示的图片
ImageProvider inactiveThumbImage关闭状态滑块显示的图片
MaterialTapTargetSize materialTapTargetSize该值与CheckBox与Radio中的作用一致
DragStartBehavior dragStartBehavior设置确定当用户启动拖动时,拖动何时正式启动

1.3 Switch示例

在这里插入图片描述

二、SwitchListTile

2.1 SwitchListTile构造器

const SwitchListTile({
    Key key,
    @required this.value,
    @required this.onChanged,
    this.activeColor,
    this.activeTrackColor,
    this.inactiveThumbColor,
    this.inactiveTrackColor,
    this.activeThumbImage,
    this.inactiveThumbImage,
    this.title,
    this.subtitle,
    this.isThreeLine = false,
    this.dense,
    this.secondary,
    this.selected = false,
  }) : _switchListTileType = _SwitchListTileType.material,
       assert(value != null),
       assert(isThreeLine != null),
       assert(!isThreeLine || subtitle != null),
       assert(selected != null),
       super(key: key);

2.2 SwitchListTile属性说明

属性说明
Widget title开关标题
Widget subtitle开关副标题
Widget secondary一个小部件,显示在与开关相对的另一侧
bool isThreeLine是否显示三行
bool dense垂直密集排列
bool selected是否将activeColor应用于title、subtitle、secondary

2.3 SwitchListTile示例

bool _switchListTitleValue = false;

Row(
	children: <Widget>[
		Flexible(
			child: SwitchListTile(
				value: _switchListTitleValue,
				title: Text('SwitchListTile'),
				subtitle: Text('subtitle'),
				secondary: Icon(Icons.close),
				isThreeLine: false,
				dense: false,
				selected: true,
				activeColor: Colors.deepPurpleAccent,
				onChanged: (data){
					setState(() {
						this._switchListTitleValue = data;
					});
				},
			),
		)
	],
)

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

LobeChat

LobeChat

AI应用

LobeChat 是一个开源、高性能的聊天机器人框架。支持语音合成、多模态和可扩展插件系统。支持一键式免费部署私人ChatGPT/LLM 网络应用程序。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MAXLZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值