news 2026/2/25 6:29:31

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/2/23 14:36:57

一键启动Fun-ASR:多语言语音识别开箱即用

一键启动Fun-ASR&#xff1a;多语言语音识别开箱即用 你是否还在为不同语言的语音转文字而烦恼&#xff1f;会议录音是粤语&#xff0c;客户电话是英文&#xff0c;产品演示又夹杂日韩内容——传统语音识别工具要么不支持&#xff0c;要么准确率惨不忍睹。现在&#xff0c;这一…

作者头像 李华
网站建设 2026/2/20 9:12:02

从零开始部署Sambert:Ubuntu 20.04环境搭建详细指南

从零开始部署Sambert&#xff1a;Ubuntu 20.04环境搭建详细指南 Sambert 多情感中文语音合成-开箱即用版&#xff0c;专为中文语音生成场景设计&#xff0c;融合了阿里达摩院先进的 Sambert-HiFiGAN 模型能力。该系统不仅支持高质量的文本转语音&#xff08;TTS&#xff09;&a…

作者头像 李华
网站建设 2026/2/5 17:01:23

智能路径规划:UAVS无人机仿真系统从入门到精通

智能路径规划&#xff1a;UAVS无人机仿真系统从入门到精通 【免费下载链接】UAVS 智能无人机路径规划仿真系统是一个具有操作控制精细、平台整合性强、全方向模型建立与应用自动化特点的软件。它以A、B两国在C区开展无人机战争为背景&#xff0c;该系统的核心功能是通过仿真平台…

作者头像 李华
网站建设 2026/2/24 23:45:01

Mac鼠标优化指南:第三方鼠标配置全攻略

Mac鼠标优化指南&#xff1a;第三方鼠标配置全攻略 【免费下载链接】mac-mouse-fix Mac Mouse Fix - A simple way to make your mouse better. 项目地址: https://gitcode.com/GitHub_Trending/ma/mac-mouse-fix Mac用户使用第三方鼠标时常面临功能限制与操作体验不佳的…

作者头像 李华