首先定义一下参数,以及左划右划的方法
double deleteButtonPosition = -120;
double renameButtonPosition = -120;
void _onHorizontalDragUpdate(DragUpdateDetails details) {
setState(() {
if (details.delta.dx < 0) {
// 左滑
deleteButtonPosition = 0; // 显示删除按钮
renameButtonPosition = 80; // 显示重命名按钮
} else if (details.delta.dx > 0) {
// 右滑
deleteButtonPosition = -120; // 隐藏删除按钮
renameButtonPosition = -160; // 隐藏重命名按钮
}
});
}
然后再在Widget中加入布局以及方法调用
onHorizontalDragUpdate: _onHorizontalDragUpdate,
AnimatedPositioned(
duration: const Duration(milliseconds: 200),
right: deleteButtonPosition,
child: InkWell(
onTap: () {
_showRenameDialog(context, widget.onRename, widget.index);
},
child: Container(
height: 100,
width: 100,
// color: Color.fromARGB(255, 106, 106, 106), // 添加背景颜色
alignment: Alignment.center,
decoration: const BoxDecoration(
color: Color.fromARGB(255, 106, 106, 106), // 添加背景颜色
borderRadius: BorderRadius.only(
topRight: Radius.circular(10),
bottomRight: Radius.circular(10),
),
),
child: const Text(
'Rename',
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
),
),