Flutter怎么实现单选,复选和开关组件
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本文小编为大家详细介绍"Flutter怎么实现单选,复选和开关组件",内容详细,步骤清晰,细节处理妥当,希望这篇"Flutter怎么实现单选,复选和开关组件"文章能帮助大家解决疑惑,下面跟着小编的思路
千家信息网最后更新 2025年11月07日Flutter怎么实现单选,复选和开关组件
本文小编为大家详细介绍"Flutter怎么实现单选,复选和开关组件",内容详细,步骤清晰,细节处理妥当,希望这篇"Flutter怎么实现单选,复选和开关组件"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
1、开关 Switch
构造方法:
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,//设置鼠标停留状态 app用不到 this.focusColor,// 获取焦点颜色 this.hoverColor,//指针悬停颜色 this.overlayColor,// 设置按压滑动覆盖上面的颜色 this.splashRadius,// 设置点击滑动覆盖圆环的半径 this.focusNode,//焦点控制 this.autofocus = false,// 是否自动获取焦点通过Switch构造方法我们可以实现简单的开关组件,并且除了改变颜色之外我们还可以自定义滑块,如果对这个开关组件进行说明除了自定义布局,还可以使用SwitchListTile组件,一个列表和Swith的组合,官方帮我们实现了很多常见的功能,可以直接拿来使用。如果使用苹果风格开关可以使用封装好的CupertinoSwitch。
示例代码:
Switch( // activeColor: Colors.blue, activeTrackColor: Colors.red, inactiveTrackColor: Colors.green, // inactiveThumbColor: Colors.yellow, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, dragStartBehavior: DragStartBehavior.start, activeThumbImage: AssetImage("images/lbxx.png"), inactiveThumbImage: AssetImage("images/lbxx.png"), value: _switchSelected, onChanged: (value) { setState(() { _switchSelected = value; }); }),2、单选 Radio
构造方法:
const Radio({ Key? key, required this.value,//单选按钮的值 required this.groupValue,//当前选中的值 required this.onChanged,//选中这个按钮的回调 this.mouseCursor,// 鼠标悬停状态 this.toggleable = false,//点击已选中按钮是否调用onChanged回调 this.activeColor,// 选项按钮颜色 this.fillColor,//设置单选框不同状态的的颜色 this.focusColor,// 获取焦点颜色 this.hoverColor,//指针悬停颜色 this.overlayColor,//按压覆盖颜色 this.splashRadius,//按压覆盖颜色的半径 this.materialTapTargetSize,//组件最小大小 this.visualDensity,//组件的紧凑程度 this.focusNode,//焦点 this.autofocus = false,//是否自动获取焦点})
单选组件使用了泛型,我们在使用的时候可以自定义选项的数据类型,一般都是在列表中使用,通过单选组件可以帮我们实现一个单选列表选项,当然Radio也有对应的RadioListTile,用来对单选框进行说明。
示例代码:
Column( children: [_radioCheckBox(_dataList[0]),_radioCheckBox(_dataList[1]),_radioCheckBox(_dataList[2]),_radioCheckBox(_dataList[3]) ],),Row _radioCheckBox(FMRadioBean fmRadioBean) { return Row( children: [ Radio( visualDensity: VisualDensity( horizontal: VisualDensity.minimumDensity, vertical: VisualDensity.minimumDensity), value: fmRadioBean, // activeColor: Colors.red, fillColor: MaterialStateProperty.resolveWith((state) { if (state.contains(MaterialState.selected)) { return Colors.red; } else { return Colors.blue; } }), focusColor: Colors.orange, groupValue: groupValue, toggleable: false, onChanged: (value) { setState(() { groupValue = fmRadioBean; radioText = fmRadioBean.text; }); }), Text(fmRadioBean.text) ], );}class FMRadioBean { int index; String text; bool isSelect; FMRadioBean(this.index, this.text, this.isSelect);} 3、复选多选 Checkbox
构造方法:
const Checkbox({ Key? key, required this.value,// 是否被选中 this.tristate = false,//复选框value是否可以为null required this.onChanged,// 选中回调 this.mouseCursor,// 鼠标指针状态 this.activeColor,// 选中颜色 this.fillColor,// 不同状态颜色设置 this.checkColor,// 对勾颜色 this.focusColor,// 获取焦点颜色 this.hoverColor,// 指针悬停颜色 this.overlayColor,// 按压覆盖颜色 this.splashRadius,// 按压覆盖半径 this.materialTapTargetSize,//最小大小 this.visualDensity,// 组件紧凑程度 this.focusNode, this.autofocus = false, this.shape,// 自定义选项框样式 this.side,// 自定义选项框边框样式})多选组件可以使用shape和side字段自定义选择框样式,不过一般交互都是单选用圆形,多选用方形。既然前面俩兄弟都有现成的辅助说明组件,多选自然也有CheckboxListTile,仨兄弟用法基本一样。
示例代码:
Column( children: [ _checkCheckBox(_dataList[0]), _checkCheckBox(_dataList[1]), _checkCheckBox(_dataList[2]), _checkCheckBox(_dataList[3]) ],),Text(_checkText.toString())Row _checkCheckBox(FMRadioBean fmRadioBean) { return Row( children: [ Checkbox( visualDensity: VisualDensity( horizontal: VisualDensity.minimumDensity, vertical: VisualDensity.minimumDensity), value: fmRadioBean.isSelect, activeColor: Colors.blue, checkColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(6)) ), side: BorderSide(color: Colors.black,width: 2,style: BorderStyle.solid), onChanged: (value) { setState(() { if (value == true) { fmRadioBean.isSelect = true; _checkText.add(fmRadioBean.text); } else { fmRadioBean.isSelect = false; _checkText.remove(fmRadioBean.text); } }); }), Text(fmRadioBean.text) ], );}读到这里,这篇"Flutter怎么实现单选,复选和开关组件"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
颜色
组件
状态
焦点
不同
图片
指针
按钮
方法
最小
代码
半径
大小
文章
样式
示例
轨道
鼠标
紧凑
兄弟
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
深度学习服务器系统
网络安全三级审计平台
阜阳点餐系统软件开发哪家好
查询数据库平台
士官网络安全的自查自纠
九思连强互联网电子科技有限公司
淮安新品联想服务器维修电话
达内软件开发培训价格
mysql 本地数据库改密
数据库插入多条
软件开发人员性格特点
数据库抽出时间默认加一年
网络安全讲座宣传文案
游戏场景服务器
摩尔庄园数据库丢失
北京服务器维修技术云主机
莱茵克拉电梯服务器说明
张家界口碑好的软件开发
扬州桦汉工控机服务器
网络安全盘是啥
x99服务器待机功耗
黄浦区信息网络技术电话
大学学网络安全报什么系
学软件开发是不是需要基礎
国际服怎么修复服务器
防诈骗、网络安全教育
网络安全演练部门
mdb数据库现在淘汰了
湖北品质软件开发设施价钱
查找数据库中某值是否存在