news 2026/9/2 22:55:52

Flutter---Sliable滑动列表项

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Flutter---Sliable滑动列表项

效果图

概念

Slidable 是一个 Flutter 包,用于创建类似 iOS 邮件应用中的滑动列表项,可以向左或向右滑动显示操作按钮。

安装

flutter_slidable: ^4.0.3

主要参数

Slidable({ Key? key, Widget? child, // 滑动的内容 SlidableController? controller, // 控制器 Axis direction = Axis.horizontal, // 滑动方向 bool enabled = true, // 是否启用滑动 bool closeOnScroll = true, // 滚动时自动关闭 double? threshold, // 触发阈值 Duration? duration, // 动画时长 // 动作面板 ActionPane? startActionPane, // 左侧动作面板 ActionPane? endActionPane, // 右侧动作面板 // 分组 String? groupTag, // 分组标签 // 监听器 ValueChanged<SlideActionType>? onSlideAction, // 滑动动作回调 ValueChanged<double>? onSlideIsOpenChanged, // 打开状态变化 })

ActionPane配置

ActionPane( motion: DrawerMotion(), // 动画类型 dismissible: DismissiblePane( // 可消失的面板 onDismissed: () {}, // 消失回调 ), extentRatio: 0.25, // 面板占宽度比例 dragDismissible: true, // 拖动可消失 children: [ // 动作按钮列表 SlidableAction(...), SlidableAction(...), ], )

关键代码

return Slidable( // 右侧滑动面板 endActionPane: ActionPane( motion: ScrollMotion(), // 滑动动画效果 children: [ // 删除动作 SlidableAction( onPressed: (context) => _deleteItem(index), //点击删除 backgroundColor: Colors.red, //背景色 foregroundColor: Colors.white, //前景色 icon: Icons.delete, //图标 spacing: 0, // 按钮间距 borderRadius: BorderRadius.circular(0), // 圆角 autoClose: true, // 点击后自动关闭面板 label: '删除', ), // 编辑动作 SlidableAction( onPressed: (context) => _editItem(index), //点击编辑 backgroundColor: Colors.blue, foregroundColor: Colors.white, icon: Icons.edit, label: '编辑', ), ], ), // 左侧滑动面板 startActionPane: ActionPane( motion: ScrollMotion(), //滑动动画效果 children: [ SlidableAction( onPressed: (context) => _shareItem(index), //点击分享 backgroundColor: Colors.green, foregroundColor: Colors.white, icon: Icons.share, label: '分享', ), SlidableAction( onPressed: (context) => _archiveItem(index), //点击归档 backgroundColor: Colors.orange, foregroundColor: Colors.white, icon: Icons.archive, label: '归档', ), ], ), //列表项内容 child: ListTile( leading: CircleAvatar( //圆形头像组件 backgroundColor: Colors.blue[100], child: Text('${index + 1}'), ), title: Text(item['title']), trailing: Icon(Icons.chevron_right), ), );

代码实例

import 'dart:async'; import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:my_flutter/ble_scanner.dart'; import 'package:my_flutter/path_manager.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State<StatefulWidget> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { //1.准备数据 final List<Map<String, dynamic>> _items = List.generate( 10, (i) => { 'id': i, 'title': '项目 ${i + 1}', }, ); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Flutter Slidable 示例')), body: ListView.builder( itemCount: _items.length, itemBuilder: (context, index) { final item = _items[index]; return Slidable( // 右侧滑动面板 endActionPane: ActionPane( motion: ScrollMotion(), // 滑动动画效果 children: [ // 删除动作 SlidableAction( onPressed: (context) => _deleteItem(index), //点击删除 backgroundColor: Colors.red, //背景色 foregroundColor: Colors.white, //前景色 icon: Icons.delete, //图标 spacing: 0, // 按钮间距 borderRadius: BorderRadius.circular(0), // 圆角 autoClose: true, // 点击后自动关闭面板 label: '删除', ), // 编辑动作 SlidableAction( onPressed: (context) => _editItem(index), //点击编辑 backgroundColor: Colors.blue, foregroundColor: Colors.white, icon: Icons.edit, label: '编辑', ), ], ), // 左侧滑动面板 startActionPane: ActionPane( motion: ScrollMotion(), //滑动动画效果 children: [ SlidableAction( onPressed: (context) => _shareItem(index), //点击分享 backgroundColor: Colors.green, foregroundColor: Colors.white, icon: Icons.share, label: '分享', ), SlidableAction( onPressed: (context) => _archiveItem(index), //点击归档 backgroundColor: Colors.orange, foregroundColor: Colors.white, icon: Icons.archive, label: '归档', ), ], ), //列表项内容 child: ListTile( leading: CircleAvatar( //圆形头像组件 backgroundColor: Colors.blue[100], child: Text('${index + 1}'), ), title: Text(item['title']), trailing: Icon(Icons.chevron_right), ), ); }, ), ); } void _deleteItem(int index) { showDialog( context: context, builder: (context) => AlertDialog( title: Text('确认删除'), content: Text('确定要删除 "${_items[index]['title']}" 吗?'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text('取消'), ), TextButton( onPressed: () { setState(() { _items.removeAt(index); }); Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('已删除')), ); }, child: Text('删除'), ), ], ), ); } void _editItem(int index) { // 编辑逻辑 } void _shareItem(int index) { // 分享逻辑 } void _archiveItem(int index) { // 归档逻辑 } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 22:45:54

小白也能懂的Unsloth教程:手把手教你训练自己的AI模型

小白也能懂的Unsloth教程&#xff1a;手把手教你训练自己的AI模型 你是不是也想过——不用博士学历、不靠顶级显卡、不读几十篇论文&#xff0c;就能让大模型听你的话&#xff1f;比如让它变成你的专属医学顾问、法律助手&#xff0c;或者能写爆款小红书文案的创意搭档&#x…

作者头像 李华
网站建设 2026/8/30 21:01:58

60、嵌入式定时器深度解析:EPIT与GPT

嵌入式定时器深度解析&#xff1a;EPIT与GPT 一、前置基础&#xff1a;定时器的“心跳”——时钟与分频倍频 定时器的本质是“对已知频率的时钟计数”&#xff0c;因此稳定的时钟源和灵活的频率调节机制&#xff08;倍频/分频&#xff09;是定时器精准工作的前提。我们先理清…

作者头像 李华
网站建设 2026/9/2 10:11:26

用Qwen-Image-Layered做的海报设计,修改效率翻倍

用Qwen-Image-Layered做的海报设计&#xff0c;修改效率翻倍 你有没有遇到过这样的情况&#xff1a;一张精心生成的海报&#xff0c;客户却只因为“换个颜色”或“调整一下位置”就要求重做&#xff1f;传统AI图像工具一旦生成完成&#xff0c;再想局部修改就得推倒重来——不…

作者头像 李华
网站建设 2026/8/29 21:00:22

5步完成系统瘦身:跨平台重复文件清理工具释放10GB+存储空间全指南

5步完成系统瘦身&#xff1a;跨平台重复文件清理工具释放10GB存储空间全指南 【免费下载链接】czkawka 一款跨平台的重复文件查找工具&#xff0c;可用于清理硬盘中的重复文件、相似图片、零字节文件等。它以高效、易用为特点&#xff0c;帮助用户释放存储空间。 项目地址: h…

作者头像 李华
网站建设 2026/9/1 20:19:33

Mac Mouse Fix效率提升指南:释放第三方鼠标全部潜能

Mac Mouse Fix效率提升指南&#xff1a;释放第三方鼠标全部潜能 【免费下载链接】mac-mouse-fix Mac Mouse Fix - A simple way to make your mouse better. 项目地址: https://gitcode.com/GitHub_Trending/ma/mac-mouse-fix 问题诊断&#xff1a;你的鼠标在macOS上是否…

作者头像 李华
网站建设 2026/9/2 22:28:00

Qwen3-4B vs Llama3实战对比:长文本理解与指令遵循性能评测教程

Qwen3-4B vs Llama3实战对比&#xff1a;长文本理解与指令遵循性能评测教程 1. 为什么这次对比值得你花15分钟读完 你是不是也遇到过这些情况&#xff1a; 给模型丢进去一篇3000字的产品需求文档&#xff0c;它却只盯着最后一段话回答&#xff1b;写了特别清楚的指令&#x…

作者头像 李华