/
main.dart
220 lines (206 loc) · 6.31 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import 'package:example/list_home.dart';
import 'package:flutter/material.dart';
import 'package:swipe_to/swipe_to.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Swipe To Example',
theme: ThemeData(
primarySwatch: Colors.blue,
primaryColor: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
iconTheme: IconThemeData(
color: Colors.blue.shade200,
),
),
home: const AppHome(),
);
}
}
class AppHome extends StatefulWidget {
const AppHome({Key? key}) : super(key: key);
@override
_AppHomeState createState() => _AppHomeState();
}
class _AppHomeState extends State<AppHome> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Swipe To Example'),
foregroundColor: Colors.white,
backgroundColor: Colors.blueGrey,
elevation: 4.0,
actions: [
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ListHome(),
),
);
},
icon: const Icon(
Icons.list_alt_rounded,
),
),
],
),
body: Container(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
///SwipeToRight Example
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SwipeTo(
child: const Align(
alignment: Alignment.centerLeft,
child: MessageViewWidget(
label: 'Hey You! Swipe me right 👉🏿',
),
),
onRightSwipe: (details) {
_displayInputBottomSheet(true);
},
),
],
),
///SwipeToLeft Example
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SwipeTo(
child: const Align(
alignment: Alignment.centerRight,
child: MessageViewWidget(
label: '👈🏿 Hey You! Swipe me Left',
),
),
onLeftSwipe: (details) {
_displayInputBottomSheet(false);
},
),
],
),
///SwipeToRight & SwipeToLeft Example
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SwipeTo(
child: const MessageViewWidget(
label: '👈🏿 Swipe me Left OR Swipe me right 👉🏿 ',
),
iconOnLeftSwipe: Icons.arrow_forward,
leftSwipeWidget: const FlutterLogo(
size: 30.0,
),
iconOnRightSwipe: Icons.arrow_back,
rightSwipeWidget: const FlutterLogo(
size: 30.0,
),
onRightSwipe: (details) {
_displayInputBottomSheet(true);
},
onLeftSwipe: (details) {
_displayInputBottomSheet(false);
},
swipeSensitivity: 10,
),
],
),
],
),
),
);
}
void _handleSwipeReply({required bool isRightSwipe, required String reply}) {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
reply,
textAlign: TextAlign.center,
),
backgroundColor:
isRightSwipe ? Colors.red.shade600 : Colors.green.shade600,
duration: const Duration(milliseconds: 1000),
),
);
}
void _displayInputBottomSheet(bool isRightSwipe) {
showModalBottomSheet(
context: context,
builder: (context) {
return Padding(
padding: MediaQuery.of(context).viewInsets,
child: Container(
padding: const EdgeInsets.only(
left: 16.0,
right: 16.0,
top: 16.0,
bottom: 16.0,
),
child: TextField(
autofocus: true,
textInputAction: TextInputAction.done,
textCapitalization: TextCapitalization.words,
onSubmitted: (value) => _handleSwipeReply(
isRightSwipe: isRightSwipe ? true : false, reply: value),
decoration: const InputDecoration(
labelText: 'Reply',
hintText: 'enter reply here',
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(
5.0,
),
),
),
),
),
),
);
},
);
}
}
class MessageViewWidget extends StatelessWidget {
const MessageViewWidget({
Key? key,
required this.label,
}) : super(key: key);
final String label;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 8.0),
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 8.0),
decoration: BoxDecoration(
color: Colors.blue.shade100,
borderRadius: const BorderRadius.all(
Radius.circular(
10.0,
),
),
),
child: Text(label));
}
}