-
-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathmain_example.dart
More file actions
225 lines (194 loc) · 4.83 KB
/
main_example.dart
File metadata and controls
225 lines (194 loc) · 4.83 KB
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
221
222
223
224
225
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'common.dart';
void main() {
runApp(MyApp());
}
const actions = [
SlideAction(
color: Color(0xFFFE4A49),
icon: Icons.delete,
label: 'Delete',
),
SlideAction(
color: Color(0xFF21B7CA),
icon: Icons.share,
label: 'Share',
),
];
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Slidable',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
AnimationController? controller;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
upperBound: 0.5,
duration: const Duration(milliseconds: 2000),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Slidable'),
),
body: SlidablePlayer(
animation: controller,
child: ListView(
children: const [
SizedBox(height: 20),
MySlidable(motion: BehindMotion()),
MySlidable(motion: StretchMotion()),
MySlidable(motion: ScrollMotion()),
MySlidable(motion: DrawerMotion()),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (controller!.isCompleted) {
controller!.reverse();
} else if (controller!.isDismissed) {
controller!.forward();
}
},
child: const Icon(Icons.play_arrow),
),
);
}
}
class SlidablePlayer extends StatefulWidget {
const SlidablePlayer({
Key? key,
required this.animation,
required this.child,
}) : super(key: key);
final Animation<double>? animation;
final Widget child;
@override
_SlidablePlayerState createState() => _SlidablePlayerState();
static _SlidablePlayerState? of(BuildContext context) {
return context.findAncestorStateOfType<_SlidablePlayerState>();
}
}
class _SlidablePlayerState extends State<SlidablePlayer> {
final Set<SlidableController?> controllers = <SlidableController?>{};
@override
void initState() {
super.initState();
widget.animation!.addListener(handleAnimationChanged);
}
@override
void dispose() {
widget.animation!.removeListener(handleAnimationChanged);
super.dispose();
}
void handleAnimationChanged() {
final value = widget.animation!.value;
controllers.forEach((controller) {
controller!.ratio = value;
});
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
class SlidableControllerSender extends StatefulWidget {
const SlidableControllerSender({
Key? key,
this.child,
}) : super(key: key);
final Widget? child;
@override
_SlidableControllerSenderState createState() =>
_SlidableControllerSenderState();
}
class _SlidableControllerSenderState extends State<SlidableControllerSender> {
SlidableController? controller;
_SlidablePlayerState? playerState;
@override
void initState() {
super.initState();
controller = Slidable.of(context);
playerState = SlidablePlayer.of(context);
playerState!.controllers.add(controller);
}
@override
void dispose() {
playerState!.controllers.remove(controller);
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child!;
}
}
class MySlidable extends StatelessWidget {
const MySlidable({
Key? key,
required this.motion,
}) : super(key: key);
final Widget motion;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8),
child: Slidable(
startActionPane: ActionPane(
motion: motion,
children: actions,
),
child: SlidableControllerSender(
child: Tile(text: motion.runtimeType.toString()),
),
),
);
}
}
class SlideAction extends StatelessWidget {
const SlideAction({
Key? key,
required this.color,
required this.icon,
required this.label,
this.flex = 1,
}) : super(key: key);
final Color color;
final IconData icon;
final int flex;
final String label;
@override
Widget build(BuildContext context) {
return SlidableAction(
flex: flex,
backgroundColor: color,
foregroundColor: Colors.white,
onPressed: (_) {},
icon: Icon(icon),
label: label,
);
}
}