Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created Feb 16, 2020
Embed
What would you like to do?
Dismissible 滑动删除
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: "滑动删除",
home: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 构建30条数据
List<String> items = List<String>.generate(30, (i) => "列表${i + 1}");
return Scaffold(
appBar: AppBar(
title: Text("滑动删除"),
),
body: ListView.builder(
// 指定列表长度
itemCount: items.length,
// 构建列表
itemBuilder: (context, index) {
final item = items[index];
// 返回一个可以被删除的列表项
return Dismissible(
key: Key(item),
// 被删除回调
onDismissed: (derection) {
// 移除指定索引项
items.removeAt(index);
// 底部弹出消息提示当前项被删除了
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("$item 被删除了"),
));
},
child: ListTile(
title: Text("$item"),
),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment