我创建了一个像这样的可关闭小部件:
GestureDetector(
onHorizontalDragUpdate: (DragUpdateDetails event) {
setState(() {
_offsetX = event.globalPosition.dx;
print("Event : ${event.globalPosition.dx}");
});
},
onHorizontalDragStart: (DragStartDetails event) {
setState(() {
_offsetX = event.globalPosition.dx;
print("Event : ${event.globalPosition.dx}");
});
print(_offsetX.abs());
// }
},
child: Dismissible(
key: UniqueKey(),
direction: DismissDirection.endToStart,
onResize: () {},
onUpdate: (details) {
// });
},
onDismissed: (direction) {},
secondaryBackground: AnimatedContainer(
duration: const Duration(milliseconds: 300),
transform: Matrix4.translationValues(_offsetX - 36, 0.0, 0.0),
color: Colors.red,
padding: const EdgeInsets.only(left: 20),
child: const Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
Icons.delete,
color: Colors.white,
size: 36,
),
// Add other widgets here that you want to move with the swipe
],
)),
background: Container(
color: DsColors.secondary,
),
confirmDismiss: (direction) async {
final result = await Future.delayed(const Duration(seconds: 6), () {
return false;
});
return result;
},
child: widget.child,
),
)
我想当交换孩子时,删除 secondaryBackground 内的图标,像交换孩子一样从右向左移动,但是当我交换孩子时 onHorizontalDragUpdate 不会被触发?
您可以从的回调中获取类似的拖动更新信息,而不是使用
onHorizontalDragUpdate
from 。GestureDetector
onUpdate
Dismissible
您还需要传递一个在构建调用之间持续存在但对该项目唯一的密钥。您应该使用带有常量值或附加项目标识符的 a ,而不是使用
UniqueKey()
及时构造的。ValueKey
例如,如果小部件位于ListView
构建器内,则需要将 包含index
在ValueKey
的值中: