@@ -27,6 +27,16 @@ class MyHomePage extends StatefulWidget {
27
27
}
28
28
29
29
class _MyHomePageState extends State <MyHomePage > {
30
+ final List <_HomeItem > items = List .generate (
31
+ 20 ,
32
+ (i) => new _HomeItem (
33
+ i,
34
+ 'Tile n°$i ' ,
35
+ _getSubtitle (i),
36
+ _getAvatarColor (i),
37
+ ),
38
+ );
39
+
30
40
@override
31
41
Widget build (BuildContext context) {
32
42
return new Scaffold (
@@ -45,32 +55,35 @@ class _MyHomePageState extends State<MyHomePage> {
45
55
itemBuilder: (context, index) {
46
56
final Axis slidableDirection =
47
57
direction == Axis .horizontal ? Axis .vertical : Axis .horizontal;
48
- if (index < 8 ) {
58
+ var item = items[index];
59
+ if (item.index < 8 ) {
49
60
return _getSlidableWithLists (context, index, slidableDirection);
50
61
} else {
51
62
return _getSlidableWithDelegates (context, index, slidableDirection);
52
63
}
53
64
},
54
- itemCount: 20 ,
65
+ itemCount: items.length ,
55
66
);
56
67
}
57
68
58
69
Widget _buildVerticalListItem (BuildContext context, int index) {
70
+ final _HomeItem item = items[index];
59
71
return new Container (
60
72
color: Colors .white,
61
73
child: new ListTile (
62
74
leading: new CircleAvatar (
63
- backgroundColor: _getAvatarColor (index) ,
64
- child: new Text ('$index ' ),
75
+ backgroundColor: item.color ,
76
+ child: new Text ('${ item . index } ' ),
65
77
foregroundColor: Colors .white,
66
78
),
67
- title: new Text ('Tile n°$ index ' ),
68
- subtitle: new Text (_getSubtitle (index) ),
79
+ title: new Text (item.title ),
80
+ subtitle: new Text (item.subtitle ),
69
81
),
70
82
);
71
83
}
72
84
73
85
Widget _buildhorizontalListItem (BuildContext context, int index) {
86
+ final _HomeItem item = items[index];
74
87
return new Container (
75
88
color: Colors .white,
76
89
width: 160.0 ,
@@ -79,15 +92,15 @@ class _MyHomePageState extends State<MyHomePage> {
79
92
children: < Widget > [
80
93
new Expanded (
81
94
child: new CircleAvatar (
82
- backgroundColor: _getAvatarColor (index) ,
83
- child: new Text ('$index ' ),
95
+ backgroundColor: item.color ,
96
+ child: new Text ('${ item . index } ' ),
84
97
foregroundColor: Colors .white,
85
98
),
86
99
),
87
100
new Expanded (
88
101
child: Center (
89
102
child: new Text (
90
- _getSubtitle (index) ,
103
+ item.subtitle ,
91
104
),
92
105
),
93
106
),
@@ -98,9 +111,24 @@ class _MyHomePageState extends State<MyHomePage> {
98
111
99
112
Widget _getSlidableWithLists (
100
113
BuildContext context, int index, Axis direction) {
114
+ final _HomeItem item = items[index];
115
+ //final int t = index;
101
116
return new Slidable (
117
+ key: new Key (item.title),
102
118
direction: direction,
103
- delegate: _getDelegate (index),
119
+ slideToDismissDelegate: new SlideToDismissDrawerDelegate (
120
+ onDismissed: (actionType) {
121
+ _showSnackBar (
122
+ context,
123
+ actionType == SlideActionType .primary
124
+ ? 'Dismiss Archive'
125
+ : 'Dimiss Delete' );
126
+ setState (() {
127
+ items.removeAt (index);
128
+ });
129
+ },
130
+ ),
131
+ delegate: _getDelegate (item.index),
104
132
actionExtentRatio: 0.25 ,
105
133
child: direction == Axis .horizontal
106
134
? _buildVerticalListItem (context, index)
@@ -139,47 +167,95 @@ class _MyHomePageState extends State<MyHomePage> {
139
167
140
168
Widget _getSlidableWithDelegates (
141
169
BuildContext context, int index, Axis direction) {
170
+ final _HomeItem item = items[index];
171
+
142
172
return new Slidable .builder (
173
+ key: new Key (item.title),
143
174
direction: direction,
144
- delegate: _getDelegate (index),
175
+ slideToDismissDelegate: new SlideToDismissDrawerDelegate (
176
+ onWillDismiss: (item.index != 10 )
177
+ ? null
178
+ : (actionType) {
179
+ return showDialog <bool >(
180
+ context: context,
181
+ builder: (context) {
182
+ return new AlertDialog (
183
+ title: new Text ('Delete' ),
184
+ content: new Text ('Item will be deleted' ),
185
+ actions: < Widget > [
186
+ new FlatButton (
187
+ child: new Text ('Cancel' ),
188
+ onPressed: () => Navigator .of (context).pop (false ),
189
+ ),
190
+ new FlatButton (
191
+ child: new Text ('Ok' ),
192
+ onPressed: () => Navigator .of (context).pop (true ),
193
+ ),
194
+ ],
195
+ );
196
+ },
197
+ );
198
+ },
199
+ onDismissed: (actionType) {
200
+ _showSnackBar (
201
+ context,
202
+ actionType == SlideActionType .primary
203
+ ? 'Dismiss Archive'
204
+ : 'Dimiss Delete' );
205
+ setState (() {
206
+ items.removeAt (index);
207
+ });
208
+ },
209
+ ),
210
+ delegate: _getDelegate (item.index),
145
211
actionExtentRatio: 0.25 ,
146
212
child: direction == Axis .horizontal
147
213
? _buildVerticalListItem (context, index)
148
214
: _buildhorizontalListItem (context, index),
149
215
actionDelegate: new SlideActionBuilderDelegate (
150
216
actionCount: 2 ,
151
- builder: (context, index, animation) {
217
+ builder: (context, index, animation, renderingMode ) {
152
218
if (index == 0 ) {
153
219
return new IconSlideAction (
154
220
caption: 'Archive' ,
155
- color: Colors .blue.withOpacity (animation.value),
221
+ color: renderingMode == SlidableRenderingMode .slide
222
+ ? Colors .blue.withOpacity (animation.value)
223
+ : (renderingMode == SlidableRenderingMode .dismiss
224
+ ? Colors .blue
225
+ : Colors .green),
156
226
icon: Icons .archive,
157
227
onTap: () => _showSnackBar (context, 'Archive' ),
158
228
);
159
229
} else {
160
230
return new IconSlideAction (
161
231
caption: 'Share' ,
162
- color: Colors .indigo.withOpacity (animation.value),
232
+ color: renderingMode == SlidableRenderingMode .slide
233
+ ? Colors .indigo.withOpacity (animation.value)
234
+ : Colors .indigo,
163
235
icon: Icons .share,
164
236
onTap: () => _showSnackBar (context, 'Share' ),
165
237
);
166
238
}
167
239
}),
168
240
secondaryActionDelegate: new SlideActionBuilderDelegate (
169
241
actionCount: 2 ,
170
- builder: (context, index, animation) {
242
+ builder: (context, index, animation, renderingMode ) {
171
243
if (index == 0 ) {
172
244
return new IconSlideAction (
173
245
caption: 'More' ,
174
- color: Colors .grey.shade200.withOpacity (animation.value),
246
+ color: renderingMode == SlidableRenderingMode .slide
247
+ ? Colors .grey.shade200.withOpacity (animation.value)
248
+ : Colors .grey.shade200,
175
249
icon: Icons .more_horiz,
176
250
onTap: () => _showSnackBar (context, 'More' ),
177
251
closeOnTap: false ,
178
252
);
179
253
} else {
180
254
return new IconSlideAction (
181
255
caption: 'Delete' ,
182
- color: Colors .red.withOpacity (animation.value),
256
+ color: renderingMode == SlidableRenderingMode .slide
257
+ ? Colors .red.withOpacity (animation.value)
258
+ : Colors .red,
183
259
icon: Icons .delete,
184
260
onTap: () => _showSnackBar (context, 'Delete' ),
185
261
);
@@ -188,7 +264,7 @@ class _MyHomePageState extends State<MyHomePage> {
188
264
);
189
265
}
190
266
191
- SlidableDelegate _getDelegate (int index) {
267
+ static SlidableDelegate _getDelegate (int index) {
192
268
switch (index % 4 ) {
193
269
case 0 :
194
270
return new SlidableBehindDelegate ();
@@ -203,7 +279,7 @@ class _MyHomePageState extends State<MyHomePage> {
203
279
}
204
280
}
205
281
206
- Color _getAvatarColor (int index) {
282
+ static Color _getAvatarColor (int index) {
207
283
switch (index % 4 ) {
208
284
case 0 :
209
285
return Colors .red;
@@ -218,7 +294,7 @@ class _MyHomePageState extends State<MyHomePage> {
218
294
}
219
295
}
220
296
221
- String _getSubtitle (int index) {
297
+ static String _getSubtitle (int index) {
222
298
switch (index % 4 ) {
223
299
case 0 :
224
300
return 'SlidableBehindDelegate' ;
@@ -237,3 +313,17 @@ class _MyHomePageState extends State<MyHomePage> {
237
313
Scaffold .of (context).showSnackBar (SnackBar (content: new Text (text)));
238
314
}
239
315
}
316
+
317
+ class _HomeItem {
318
+ const _HomeItem (
319
+ this .index,
320
+ this .title,
321
+ this .subtitle,
322
+ this .color,
323
+ );
324
+
325
+ final int index;
326
+ final String title;
327
+ final String subtitle;
328
+ final Color color;
329
+ }
0 commit comments