当前位置: 首页 > news >正文

Flutter 多功能列表项:图标、文字与Switch组合

下面是一个完整的实现,包含左边图标、中间标题和右边 Switch 的组件,背景为白色:

完整组件代码

import 'package:flutter/material.dart';class IconTitleSwitch extends StatelessWidget {final IconData icon;final String title;final bool value;final ValueChanged<bool> onChanged;final Color iconColor;final Color? backgroundColor;final EdgeInsetsGeometry padding;const IconTitleSwitch({Key? key,required this.icon,required this.title,required this.value,required this.onChanged,this.iconColor = Colors.blue,this.backgroundColor = Colors.white,this.padding = const EdgeInsets.symmetric(horizontal: 16, vertical: 12),}) : super(key: key);Widget build(BuildContext context) {return Container(color: backgroundColor,padding: padding,child: Row(children: [// 左侧图标Icon(icon,color: iconColor,size: 24,),const SizedBox(width: 16),// 中间标题(自动扩展)Expanded(child: Text(title,style: const TextStyle(fontSize: 16,color: Colors.black,),),),// 右侧SwitchSwitch(value: value,onChanged: onChanged,activeColor: Colors.blue,),],),);}
}

使用示例

class ExamplePage extends StatefulWidget {const ExamplePage({Key? key}) : super(key: key);_ExamplePageState createState() => _ExamplePageState();
}class _ExamplePageState extends State<ExamplePage> {bool _notificationEnabled = true;bool _darkModeEnabled = false;bool _autoUpdateEnabled = true;Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('设置'),),body: Column(children: [// 使用示例1IconTitleSwitch(icon: Icons.notifications,title: '消息通知',value: _notificationEnabled,onChanged: (value) {setState(() {_notificationEnabled = value;});},),// 分割线const Divider(height: 1),// 使用示例2IconTitleSwitch(icon: Icons.dark_mode,title: '夜间模式',value: _darkModeEnabled,onChanged: (value) {setState(() {_darkModeEnabled = value;});},iconColor: Colors.purple,),// 分割线const Divider(height: 1),// 使用示例3IconTitleSwitch(icon: Icons.system_update,title: '自动更新',value: _autoUpdateEnabled,onChanged: (value) {setState(() {_autoUpdateEnabled = value;});},iconColor: Colors.green,),],),);}
}

组件特性说明

  1. 可定制属性:
    · icon: 左侧显示的图标
    · title: 中间显示的标题文本
    · value: Switch 的当前值
    · onChanged: Switch 状态改变回调
    · iconColor: 图标颜色(默认蓝色)
    · backgroundColor: 背景颜色(默认白色)
    · padding: 内边距(默认水平16,垂直12)
  2. 布局特点:
    · 使用 Row 水平排列图标、标题和 Switch
    · 标题使用 Expanded 自动填充可用空间
    · 默认添加白色背景
    · 组件高度自适应内容
  3. 扩展建议:
    · 如果需要添加副标题,可以在 Text 下方添加一个 Text 小部件
    · 如果需要圆角背景,可以给 Container 添加 borderRadius
    · 如果需要点击整个区域的效果,可以用 InkWell 包裹

效果预览

这个组件会呈现如下效果:

[图标]  标题文本                  [Switch]

所有元素排列在一行,左侧为图标,中间为标题(自动扩展填充空间),右侧为 Switch 开关,整个组件背景为白色。

http://www.lryc.cn/news/624776.html

相关文章:

  • 在阿里云 CentOS Stream 9 64位 UEFI 版上离线安装 Docker Compose
  • 智能人形机器人:知识驱动的工业生产力革新
  • 神经网络显存占用分析:从原理到优化的实战指南
  • 实战架构思考及实战问题:Docker+‌Jenkins 自动化部署
  • 【论文阅读】-《GeoDA: a geometric framework for black-box adversarial attacks》
  • 动态规划:入门思考篇
  • 01.Linux小技巧
  • 【Python语法基础学习笔记】条件表达式和逻辑表达式
  • python遇到异常流程
  • 【verge3d】如何在项目里调用接口
  • Python函数:装饰器
  • Kafka 零拷贝(Zero-Copy)技术详解
  • C++面试中的手写快速排序:从基础到最优的完整思考过程
  • IEC EN 62040 不间断电源系统(UPS)安全要求标准
  • 【音视频】芯片、方案、市场信息收集
  • 恒创科技:日本服务器 ping 不通?从排查到解决的实用指南
  • 政策技术双轮驱动智慧灯杆市场扩容,塔能科技破解行业痛点
  • 【轨物交流】轨物科技与华为鲲鹏生态深度合作 光伏清洁机器人解决方案获技术认证!
  • 微算法科技(NASDAQ: MLGO)研究分片技术:重塑区块链可扩展性新范式
  • 【P38 6】OpenCV Python——图片的运算(算术运算、逻辑运算)加法add、subtract减法、乘法multiply、除法divide
  • Maven resources资源配置详解
  • 深度研究系统、方法与应用的综述
  • kubeadm方式部署k8s集群
  • zsh 使用笔记 命令行智能提示 bash智能
  • 视频因为264问题无法网页播放,解决方案之一:转化视频
  • 【matlab】考虑源荷不平衡的微电网鲁棒定价研究
  • 第7节 神经网络
  • grep命令要点、详解和示例
  • 淘宝扭蛋机小程序开发:引领电商娱乐化新潮流
  • 剧本杀小程序系统开发:保障游戏公平,营造健康娱乐环境