|
| 1 | +## 介绍 |
| 2 | + |
| 3 | +Dismissible 是一种可以通过沿指定的方向拖动来关闭的 Widget。 |
| 4 | +比如,若我们需要向左或者向右滑动来移除一个Widget(比如很常见的“滑动删除”功能),则非常适合使用Dismissible来实现。 |
| 5 | + |
| 6 | +## 继承关系 |
| 7 | + |
| 8 | +Object -> Diagnosticable -> DiagnosticableTree -> Widget -> StatefulWidget -> Dismissible |
| 9 | + |
| 10 | +## 构造函数 |
| 11 | + |
| 12 | +``` |
| 13 | +Dismissible({@required Key key, |
| 14 | +@required Widget child, |
| 15 | +Widget background, |
| 16 | +Widget secondaryBackground, |
| 17 | +ConfirmDismissCallback confirmDismiss, |
| 18 | +VoidCallback onResize, |
| 19 | +DismissDirectionCallback onDismissed, |
| 20 | +DismissDirection direction: DismissDirection.horizontal, |
| 21 | +Duration resizeDuration: const Duration(milliseconds: 300), |
| 22 | +Map<DismissDirection, double> dismissThresholds: const {}, |
| 23 | +Duration movementDuration: const Duration(milliseconds: 200), |
| 24 | +double crossAxisEndOffset: 0.0, |
| 25 | +DragStartBehavior dragStartBehavior: DragStartBehavior.start }) |
| 26 | +``` |
| 27 | + |
| 28 | +## 常用属性 |
| 29 | + |
| 30 | +- background → Widget |
| 31 | +背景部件。如果还指定了secondaryBackground,则仅在子项被向下或向右拖动时才显示此小部件。 |
| 32 | + |
| 33 | +- child → Widget 子部件。 |
| 34 | + |
| 35 | + - confirmDismiss → ConfirmDismissCallback |
| 36 | +使应用有机会确认或取消的回调。 |
| 37 | + |
| 38 | +- crossAxisEndOffset → double |
| 39 | +定义取消卡片后横过主轴的结束偏移量 |
| 40 | + |
| 41 | +- direction → DismissDirection |
| 42 | +可以关闭 Widget 的方向。 |
| 43 | + |
| 44 | +- dismissThresholds → Map<DismissDirection, double> |
| 45 | +必须拖动项目才能被视为已取消的偏移阈值 |
| 46 | + |
| 47 | +- dragStartBehavior → DragStartBehavior |
| 48 | +确定处理拖动开始行为的方式。 |
| 49 | + |
| 50 | +- movementDuration → Duration |
| 51 | +定义了卡片退回或未退回原位置的持续时间。 |
| 52 | + |
| 53 | +- onDismissed → DismissDirectionCallback |
| 54 | +完成调整大小后,在关闭窗口小部件时调用。 |
| 55 | + |
| 56 | +- onResize → VoidCallback |
| 57 | +当窗口小部件更改大小时调用(即在关闭前收缩时调用)。 |
| 58 | + |
| 59 | +- resizeDuration → Duration |
| 60 | +在调用onDismissed之前,窗口小部件所需花费的收缩时间。 |
| 61 | + |
| 62 | +- secondaryBackground → Widget |
| 63 | +堆叠在子部件后面的背景部件,当子部件被向上或向左拖动时会暴露出来。只有在同时指定了背景时才能指定它。 |
| 64 | + |
| 65 | + |
| 66 | +## 使用示例 (实现滑动删除的 sample) |
| 67 | + |
| 68 | +(参考地址 https://flutterchina.club/cookbook/gestures/dismissible/ |
| 69 | +) |
| 70 | +### 1. 创建item列表 |
| 71 | + |
| 72 | +第一步是创建一个我们可以滑动的列表。有关如何创建列表的更多详细说明,请按照使用长列表进行操作。 |
| 73 | + |
| 74 | +#### 创建数据源 |
| 75 | + |
| 76 | +简单起见,这里生成一个字符串列表。 |
| 77 | + |
| 78 | +final items = new List<String>.generate(20, (i) => "Item ${i + 1}"); |
| 79 | + |
| 80 | +#### 将数据源转换为List |
| 81 | + |
| 82 | +首先,我们将简单地在屏幕上的列表中显示每个项目(先不支持滑动)。 |
| 83 | + |
| 84 | +``` |
| 85 | +new ListView.builder( |
| 86 | + itemCount: items.length, |
| 87 | + itemBuilder: (context, index) { |
| 88 | + return new ListTile(title: new Text('${items[index]}')); |
| 89 | + }, |
| 90 | +); |
| 91 | +``` |
| 92 | + |
| 93 | +### 2. 将item包装在Dismissible Widget中 |
| 94 | + |
| 95 | +现在我们希望让用户能够将条目从列表中移除,用户删除一个条目后,我们需要从列表中删除该条目并显示一个Snackbar。 在实际的场景中,您可能需要执行更复杂的逻辑,例如从Web服务或数据库中删除条目。 |
| 96 | + |
| 97 | +这是我们就可以使用Dismissable。在下面的例子中,我们将更新itemBuilder函数以返回一个DismissableWidget。 |
| 98 | + |
| 99 | +``` |
| 100 | +new Dismissible( |
| 101 | + // Each Dismissible must contain a Key. Keys allow Flutter to |
| 102 | + // uniquely identify Widgets. |
| 103 | + key: new Key(item), |
| 104 | + // We also need to provide a function that will tell our app |
| 105 | + // what to do after an item has been swiped away. |
| 106 | + onDismissed: (direction) { |
| 107 | + // Remove the item from our data source |
| 108 | + items.removeAt(index); |
| 109 | +
|
| 110 | + // Show a snackbar! This snackbar could also contain "Undo" actions. |
| 111 | + Scaffold.of(context).showSnackBar( |
| 112 | + new SnackBar(content: new Text("$item dismissed"))); |
| 113 | + }, |
| 114 | + child: new ListTile(title: new Text('$item')), |
| 115 | +); |
| 116 | +``` |
| 117 | + |
| 118 | +### 3. 提供滑动背景提示 |
| 119 | + |
| 120 | +现在,我们的应用程序将允许用户从列表中滑动项目,但用户并不知道滑动后做了什么,所以,我们需要告诉用户滑动操作会移除条目。 为此,我们将在滑动条目时显示指示。在下面的例子中,我们通过将背景设置为红色表示为删除操作。 |
| 121 | + |
| 122 | +为此,我们为Dismissable提供一个background参数。 |
| 123 | +``` |
| 124 | +new Dismissible( |
| 125 | + // Show a red background as the item is swiped away |
| 126 | + background: new Container(color: Colors.red), |
| 127 | + key: new Key(item), |
| 128 | + onDismissed: (direction) { |
| 129 | + items.removeAt(index); |
| 130 | +
|
| 131 | + Scaffold.of(context).showSnackBar( |
| 132 | + new SnackBar(content: new Text("$item dismissed"))); |
| 133 | + }, |
| 134 | + child: new ListTile(title: new Text('$item')), |
| 135 | +); |
| 136 | +``` |
| 137 | + |
| 138 | + |
| 139 | +### 完整代码: |
| 140 | + |
| 141 | +``` |
| 142 | +import 'package:flutter/foundation.dart'; |
| 143 | +import 'package:flutter/material.dart'; |
| 144 | +
|
| 145 | +void main() { |
| 146 | + runApp(new MyApp( |
| 147 | + items: new List<String>.generate(20, (i) => "Item ${i + 1}"), |
| 148 | + )); |
| 149 | +} |
| 150 | +
|
| 151 | +class MyApp extends StatelessWidget { |
| 152 | + final List<String> items; |
| 153 | +
|
| 154 | + MyApp({Key key, @required this.items}) : super(key: key); |
| 155 | +
|
| 156 | + @override |
| 157 | + Widget build(BuildContext context) { |
| 158 | + final title = 'Dismissing Items'; |
| 159 | +
|
| 160 | + return new MaterialApp( |
| 161 | + title: title, |
| 162 | + home: new Scaffold( |
| 163 | + appBar: new AppBar( |
| 164 | + title: new Text(title), |
| 165 | + ), |
| 166 | + body: new ListView.builder( |
| 167 | + itemCount: items.length, |
| 168 | + itemBuilder: (context, index) { |
| 169 | + final item = items[index]; |
| 170 | +
|
| 171 | + return new Dismissible( |
| 172 | + // Each Dismissible must contain a Key. Keys allow Flutter to |
| 173 | + // uniquely identify Widgets. |
| 174 | + key: new Key(item), |
| 175 | + // We also need to provide a function that will tell our app |
| 176 | + // what to do after an item has been swiped away. |
| 177 | + onDismissed: (direction) { |
| 178 | + items.removeAt(index); |
| 179 | +
|
| 180 | + Scaffold.of(context).showSnackBar( |
| 181 | + new SnackBar(content: new Text("$item dismissed"))); |
| 182 | + }, |
| 183 | + // Show a red background as the item is swiped away |
| 184 | + background: new Container(color: Colors.red), |
| 185 | + child: new ListTile(title: new Text('$item')), |
| 186 | + ); |
| 187 | + }, |
| 188 | + ), |
| 189 | + ), |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
0 commit comments