标签:false 滑块 Colors value 开关 Switch 组件 switchValue Flutter
一 概述
本文介绍Flutter中的开关控件:
- Switch:只有开关功能,打开/关系开关
- SwitchListTile:带有文字描述和开关的开关控件
- CupertinoSwitch:IOS风格的开关控件
二 Switch-只有开关
2.1 构造方法
const Switch({
Key? key,
required this.value,
required this.onChanged,
this.activeColor,
this.activeTrackColor,
this.inactiveThumbColor,
this.inactiveTrackColor,
this.activeThumbImage,
this.onActiveThumbImageError,
this.inactiveThumbImage,
this.onInactiveThumbImageError,
this.thumbColor,
this.trackColor,
this.materialTapTargetSize,
this.dragStartBehavior = DragStartBehavior.start,
this.mouseCursor,
this.focusColor,
this.hoverColor,
this.overlayColor,
this.splashRadius,
this.focusNode,
this.autofocus = false,
})
2.2 常用属性
属性 | 说明 | 取值 |
---|---|---|
value | 开关是否打开 | bool对象 |
activeTrackColor | 滑块轨迹颜色 | Colors对象 |
activeColor | 滑块打开后颜色(为图像时,不显示) | Colors对象 |
inactiveTrackColor | 滑块未打开时轨迹颜色 | Colors对象 |
inactiveThumbColor | 滑块未打开时颜色 | Colors对象 |
activeThumbImage | 滑块打开后的图标 | ImageProvider对象 |
2.3 示例
代码
var _switchValue = false;
Switch(
activeThumbImage: AssetImage('images/bird.png',),
inactiveThumbColor: Colors.green,
inactiveTrackColor: Colors.orange,
activeColor: Colors.red,
activeTrackColor: Colors.blue,
value: _switchValue,
onChanged: (value) {
setState(() {
_switchValue = value;
});
})
效果图
三 SwitchListTile-开关+文字
3.1 构造方法
const SwitchListTile({
Key? key,
required this.value,
required this.onChanged,
this.tileColor,
this.activeColor,
this.activeTrackColor,
this.inactiveThumbColor,
this.inactiveTrackColor,
this.activeThumbImage,
this.inactiveThumbImage,
this.title,
this.subtitle,
this.isThreeLine = false,
this.dense,
this.contentPadding,
this.secondary,
this.selected = false,
this.autofocus = false,
this.controlAffinity = ListTileControlAffinity.platform,
this.shape,
this.selectedTileColor,
})
3.2 常用属性
属性 | 说明 | 取值 |
---|---|---|
value | 开关是否打开 | bool对象 |
activeTrackColor | 滑块轨迹颜色 | Colors对象 |
activeColor | 滑块打开后颜色(为图像时,不显示) | Colors对象 |
inactiveTrackColor | 滑块未打开时轨迹颜色 | Colors对象 |
inactiveThumbColor | 滑块未打开时颜色 | Colors对象 |
activeThumbImage | 滑块打开后的图标 | ImageProvider对象 |
title | 标题 | Widget |
subtitle | 子标题 | Widget |
3.3 示例
代码
var _switchListValue = false;
SwitchListTile(
title: Text("开启消息推送?"),
subtitle: Text("subTitle"),
value: _switchListValue,
onChanged: (value) {
setState(() {
_switchListValue = value;
});
})
效果图
四 CupertinoSwitch-仿IOS
4.1 构造方法
const CupertinoSwitch({
Key? key,
required this.value,
required this.onChanged,
this.activeColor,
this.trackColor,
this.dragStartBehavior = DragStartBehavior.start,
})
4.2 示例
var _switchValue = false;
CupertinoSwitch(
value: _switchValue,
onChanged: (value) {
setState(() {
_switchValue = value;
});
})
效果图
标签:false,滑块,Colors,value,开关,Switch,组件,switchValue,Flutter 来源: https://blog.csdn.net/Calvin_zhou/article/details/115671386
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。