当前位置: 首页 > 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.dtcms.com/a/337183.html

相关文章:

  • Highcharts for Flutter 正式发布
  • 云原生俱乐部-docker知识点归纳(1)
  • MySQL的多版本并发控制(MVCC):
  • 【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
  • 基于 JSP+Mysql实现MVC房屋租赁系统
  • Word 中 MathType 公式编号问题与解决
  • 自定义table
  • SpringBoot--Spring MVC 拦截器注入与 new 的区别
  • OSCP - Proving Grounds - Vanity
  • 分布式系统的“不可能三角”:CAP定理深度解析
  • 【数据结构】堆中插入数据,删除数据
  • Android Handler 线程执行机制
  • python遇到异常流程
  • 【Langchain系列二】LangChain+Prompt +LLM智能问答入门
  • Unix Domain Socket(UDS)和 TCP/IP(使用 127.0.0.1)进程间通信(IPC)的比较
  • Baumer高防护相机如何通过YoloV8深度学习模型实现网球运动员和网球速度的检测分析(C#代码UI界面版)
  • Spring AI Alibaba 项目接入兼容 OpenAI API 的大模型
  • Midjourney绘画创作入门操作创作(在线教育背景系列)
  • IOPaint 远程修图:cpolar 内网穿透服务实现跨设备图片编辑
  • Ansible文件部署与大项目多主机管理
  • 【Git】windows系统删除同名路径
  • 机器翻译论文阅读方法:顶会(ACL、EMNLP)论文解析技巧
  • IntelliJ IDEA中Maven的“Sync“与“Reload“详解:小白的避坑指南
  • python DataFrame基础操作
  • 从零开始部署经典开源项目管理系统最新版redmine6-Linux Debian12
  • 链路聚合路由器OpenMPTCProuter源码编译与运行
  • 如何在 Ubuntu 24.04 安装 Python 3.12 ?
  • SpringAI集成MCP
  • 详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
  • 【金融数据分析】用Python对金融产品价格进行时间序列分解