实现滑动关闭
“滑动删除”模式在许多移动应用中很常见。例如,在编写电子邮件应用时,你可能希望允许用户通过滑动来删除列表中的电子邮件。
Flutter 通过提供 Dismissible
组件使此任务变得容易。通过以下步骤学习如何实现滑动删除:
- 创建项目列表。
- 将每个项目包装在
Dismissible
组件中。 - 提供“留下”指示器。
1. 创建项目列表
#首先,创建一个项目列表。有关如何创建列表的详细说明,请参阅处理长列表指南。
创建数据源
#在此示例中,你需要处理 20 个示例项目。为简单起见,生成一个字符串列表。
dart
final items = List<String>.generate(20, (i) => 'Item ${i + 1}');
将数据源转换为列表
#在屏幕上显示列表中的每个项目。用户还无法滑动移除这些项目。
dart
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)
2. 将每个项目包装在 Dismissible 组件中
#在此步骤中,通过使用 Dismissible
组件,让用户能够将项目从列表中滑出。
用户滑动移除项目后,将其从列表中删除并显示一个 Snackbar(信息提示条)。在实际应用中,你可能需要执行更复杂的逻辑,例如从网络服务或数据库中删除该项目。
更新 itemBuilder()
函数以返回一个 Dismissible
组件
dart
itemBuilder: (context, index) {
final item = items[index];
return Dismissible(
// Each Dismissible must contain a Key. Keys allow Flutter to
// uniquely identify widgets.
key: Key(item),
// Provide a function that tells the app
// what to do after an item has been swiped away.
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
items.removeAt(index);
});
// Then show a snackbar.
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('$item dismissed')));
},
child: ListTile(title: Text(item)),
);
},
3. 提供后台指示器
#目前,应用允许用户滑动移除列表中的项目,但没有提供视觉指示,说明滑动后会发生什么。为了提示项目已被移除,当用户将项目滑出屏幕时,显示一个“留下”指示器。在此示例中,该指示器是一个红色背景。
要添加指示器,请为 Dismissible
提供一个 background
参数。
dart
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$item dismissed')));
},
// Show a red background as the item is swiped away.
background: Container(color: Colors.red),
child: ListTile(
title: Text(item),
),
互动示例
#import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
// MyApp is a StatefulWidget. This allows updating the state of the
// widget when an item is removed.
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
final items = List<String>.generate(20, (i) => 'Item ${i + 1}');
@override
Widget build(BuildContext context) {
const title = 'Dismissing Items';
return MaterialApp(
title: title,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: Scaffold(
appBar: AppBar(title: const Text(title)),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Dismissible(
// Each Dismissible must contain a Key. Keys allow Flutter to
// uniquely identify widgets.
key: Key(item),
// Provide a function that tells the app
// what to do after an item has been swiped away.
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
items.removeAt(index);
});
// Then show a snackbar.
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('$item dismissed')));
},
// Show a red background as the item is swiped away.
background: Container(color: Colors.red),
child: ListTile(title: Text(item)),
);
},
),
),
);
}
}