【Flutter 专题】117 图解 Dismissible 滑动清除 Widget

本文介绍了如何在Flutter中使用DismissibleWidget实现自定义滑动关闭功能,包括设置不同的背景(primary和secondary)、控制滑动方向、以及onDismissed和confirmDismiss回调的使用。通过实例代码展示了如何在ListView中应用这些特性。
摘要由CSDN通过智能技术生成

this.secondaryBackground, // 与background相反滑动背景展示 Widget
this.confirmDismiss, // 是否确定清除当前 Widget
this.onResize, // 重新修改尺寸回调
this.onDismissed, // 确定清除回调
this.direction = DismissDirection.horizontal, // 滑动关闭方向
this.resizeDuration = const Duration(milliseconds: 300), // 修改尺寸时长
this.dismissThresholds = const <DismissDirection, double>{}, // 各方向滑动清除阀值
this.movementDuration = const Duration(milliseconds: 200), // 清除过程时长
this.crossAxisEndOffset = 0.0,
this.dragStartBehavior = DragStartBehavior.start,
})

分析源码可得,其中 key 是必须参数,key 作为 Widget 的唯一标识,对应滑动删除的组件,在列表中不建议直接用索引作为 key 的唯一标识,因为关闭窗口小部件可能会更改其他窗口小部件的索引;

案例源码

_listWid() => ListView.builder(itemCount: 20, itemBuilder: (context, index) => _disItem(index));

_listItem(index) {
return Column(children: [
Container( height: 55.0,
margin: EdgeInsets.symmetric(horizontal: 20),
child: Row(children: [
Expanded(child: Text(‘当前 item = ${(index + 1)}’, style: TextStyle(fontSize: 16.0))),
Padding(child: Icon(Icons.lock_open, size: 14.0), padding: EdgeInsets.only(left: 10.0))
])),
Container(height: 0.5, color: Colors.grey)
]);
}

_disItem(index) => Dismissible(key: UniqueKey(), child: _listItem(index));

1. background

background 为堆叠在 Dismissible 设置的 child 元素后的 Widget,在滑动过程中展示的背景 Widget;对于未设置 secondaryBackground 时,各个方向的滑动展示的背景均为 background

_backgroundWid() {
return Container( color: Colors.red,
child: Align( alignment: Alignment.centerRight,
child: Padding( padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.delete_forever, color: Colors.white, size: 18.0),
SizedBox(height: 5.0),
Text(‘Delete’, style: TextStyle(color: Colors.white))
]))));
}

2. secondaryBackground

secondaryBackgroundbackground 类似,仅支持在向上滑动或从右向左滑动方向上展示的背景 Widget

_secondBackgroundWid() {
return Container( color: Colors.green,
child: Align( alignment: Alignment.centerLeft,
child: Padding( padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.edit, color: Colors.white, size: 18.0),
SizedBox(height: 5.0),
Text(‘Edit’,

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值