news 2026/5/1 6:36:16

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/4/30 22:48:08

Sambert语音合成API封装:Python Flask服务部署实战

Sambert语音合成API封装&#xff1a;Python Flask服务部署实战 1. 开箱即用的多情感中文语音合成体验 你有没有遇到过这样的场景&#xff1a;需要为产品视频配上自然的中文配音&#xff0c;但专业录音成本高、周期长&#xff1b;或者想快速验证一段文案的语音效果&#xff0c…

作者头像 李华
网站建设 2026/5/1 7:16:30

用测试镜像简化OpenWrt自启流程,省时又省力

用测试镜像简化OpenWrt自启流程&#xff0c;省时又省力 你是不是也经历过这样的场景&#xff1a;在OpenWrt路由器上部署一个服务后&#xff0c;每次重启都要手动启动&#xff0c;反复执行/etc/init.d/myservice start&#xff1b;或者好不容易写好rc.local脚本&#xff0c;却因…

作者头像 李华
网站建设 2026/5/1 7:07:37

参数量更低但效果更强!lama轻量化设计亮点

参数量更低但效果更强&#xff01;lama轻量化设计亮点 1. 引言&#xff1a;图像修复的新思路 你有没有遇到过这样的情况&#xff1f;一张珍贵的老照片上有划痕&#xff0c;或者截图里带着不想保留的水印&#xff0c;又或者合影中出现了不该在的人。过去处理这些问题要么靠手动…

作者头像 李华
网站建设 2026/4/23 9:53:43

告别安装地狱!2023云端PDF处理全攻略:3分钟上手的效率神器

告别安装地狱&#xff01;2023云端PDF处理全攻略&#xff1a;3分钟上手的效率神器 【免费下载链接】PDFPatcher PDF补丁丁——PDF工具箱&#xff0c;可以编辑书签、剪裁旋转页面、解除限制、提取或合并文档&#xff0c;探查文档结构&#xff0c;提取图片、转成图片等等 项目地…

作者头像 李华
网站建设 2026/5/1 7:25:12

开源磁盘清理工具Czkawka:告别磁盘臃肿的空间释放指南

开源磁盘清理工具Czkawka&#xff1a;告别磁盘臃肿的空间释放指南 【免费下载链接】czkawka 一款跨平台的重复文件查找工具&#xff0c;可用于清理硬盘中的重复文件、相似图片、零字节文件等。它以高效、易用为特点&#xff0c;帮助用户释放存储空间。 项目地址: https://git…

作者头像 李华