-
Notifications
You must be signed in to change notification settings - Fork 188
/
dismissible_page.dart
149 lines (137 loc) · 4.73 KB
/
dismissible_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import 'package:flutter/material.dart';
class DismissiblePage extends StatefulWidget {
@override
_DismissiblePageState createState() => _DismissiblePageState();
}
class _DismissiblePageState extends State<DismissiblePage> {
// 生成列表数据
var _listData = List<String>.generate(30, (i) => 'Items $i');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DismissiblePage'),
),
body: _createListView(),
);
}
// 创建ListView
Widget _createListView() {
return ListView.builder(
itemCount: _listData.length,
itemBuilder: (context, index) {
return Dismissible(
// Key
key: Key('key${_listData[index]}'),
// Child
child: ListTile(
title: Text('${_listData[index]}'),
),
onDismissed: (direction) {
var _snackStr;
if (direction == DismissDirection.endToStart) {
// 从右向左 也就是删除
_snackStr = '删除了${_listData[index]}';
} else if (direction == DismissDirection.startToEnd) {
_snackStr = '收藏了${_listData[index]}';
}
// 展示 SnackBar
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(_snackStr),
duration: Duration(milliseconds: 400),
));
// 删除后刷新列表,以达到真正的删除
setState(() {
_listData.removeAt(index);
});
},
background: Container(
color: Colors.green,
// 这里使用 ListTile 因为可以快速设置左右两端的Icon
child: ListTile(
leading: Icon(
Icons.bookmark,
color: Colors.white,
),
),
),
secondaryBackground: Container(
color: Colors.red,
// 这里使用 ListTile 因为可以快速设置左右两端的Icon
child: ListTile(
trailing: Icon(
Icons.delete,
color: Colors.white,
),
),
),
confirmDismiss: (direction) async {
var _confirmContent;
var _alertDialog;
if (direction == DismissDirection.endToStart) {
// 从右向左 也就是删除
_confirmContent = '确认删除${_listData[index]}?';
_alertDialog = _createDialog(
_confirmContent,
() {
// 展示 SnackBar
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('确认删除${_listData[index]}'),
duration: Duration(milliseconds: 400),
));
Navigator.of(context).pop(true);
},
() {
// 展示 SnackBar
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('不删除${_listData[index]}'),
duration: Duration(milliseconds: 400),
));
Navigator.of(context).pop(false);
},
);
} else if (direction == DismissDirection.startToEnd) {
_confirmContent = '确认收藏${_listData[index]}?';
_alertDialog = _createDialog(
_confirmContent,
() {
// 展示 SnackBar
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('确认收藏${_listData[index]}'),
duration: Duration(milliseconds: 400),
));
Navigator.of(context).pop(true);
},
() {
// 展示 SnackBar
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('不收藏${_listData[index]}'),
duration: Duration(milliseconds: 400),
));
Navigator.of(context).pop(false);
},
);
}
var isDismiss = await showDialog(
context: context,
builder: (context) {
return _alertDialog;
});
return isDismiss;
},
);
},
);
}
Widget _createDialog(
String _confirmContent, Function sureFunction, Function cancelFunction) {
return AlertDialog(
title: Text('Confirm'),
content: Text(_confirmContent),
actions: <Widget>[
FlatButton(onPressed: sureFunction, child: Text('sure')),
FlatButton(onPressed: cancelFunction, child: Text('cancel')),
],
);
}
}