【Flutter 专题】117 图解 Dismissible 滑动清除 Widget(1)
this.confirmDismiss, // 是否确定清除当前 Widgetthis.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: <Widget>[Container( height: 55.0,margin: EdgeInsets.symmetric(horizontal: 20),child: Row(children: <Widget>[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: <Widget>[Icon(Icons.delete_forever, color: Colors.white, size: 18.0),SizedBox(height: 5.0),Text('Delete', style: TextStyle(color: Colors.white))]))));}
2. secondaryBackground
secondaryBackground 与 background 类似,仅支持在向上滑动或从右向左滑动方向上展示的背景 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: <Widget>[Icon(Icons.edit, color: Colors.white, size: 18.0),SizedBox(height: 5.0),Text('Edit', style: TextStyle(color: Colors.white))]))));}
3. direction
direction 为限制滑动关闭方向,在 onDismissed / confirmDismiss 中也可以进行判断;
DismissDirection.vertical 竖直方向,包括 up / down 两种
DismissDirection.horizontal 水平方向,包括 endToStart / startToEnd 两种
DismissDirection.endToStart 结束到开始方向(与语言设置的 rtl 和 ltr 相关),汉英等日常方向一般是从右至左
DismissDirection.startToEnd 开始到结束方向(与语言设置的 rtl 和 ltr 相关),汉英等日常方向一般是从左至右
DismissDirection.up 由下向上
DismissDirection.down 由上向下
return Padding(padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),child: Dismissible(key: Key('{_direction.toString()}', style: TextStyle(color: Colors.white, fontSize: 16.0))),decoration: BoxDecoration(color: _color, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(20.0))),direction: _direction,background: Container(decoration: BoxDecoration(color: Colors.grey, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(20.0))),));
![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/af75e9f55627486eb27117
71e9ac5825~tplv-k3u1fbpfcp-zoom-1.image)
评论