Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created February 16, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlid/6bbe82c7e20f093c651b6b0fc7a1e2f0 to your computer and use it in GitHub Desktop.
Save ryanlid/6bbe82c7e20f093c651b6b0fc7a1e2f0 to your computer and use it in GitHub Desktop.
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