先看效果:
就是上面的效果,觉得有用吗?
实现代码如下:
import 'package:flutter/material.dart';
//列表左右滑动删除
void main() => runApp(
MaterialApp(
home: HomePage(),
)
);
class HomePage extends StatelessWidget {
final List<String> items = List.generate(20,(index)=> 'item $index');
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 20,
itemBuilder: (context,index){
final item = items[index];
return Dismissible(
onDismissed: (_){ //参数暂时没有用到,则用下划线表示
items.removeAt(index);
}, // 监听
movementDuration: Duration(milliseconds: 100),
key: Key(item),
child: ListTile(
title: Text('$item'),
),
background: Container(
color: Color(0xffff0000),
),
);
},
),
);
}
}
好了,代码是不是很少?去试试吧!