forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimating-fab-in-flutter.dart
186 lines (167 loc) Ā· 4.19 KB
/
animating-fab-in-flutter.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
// š¦ Twitter https://twitter.com/vandadnp
// šµ LinkedIn https://linkedin.com/in/vandadnp
// š„ YouTube https://youtube.com/c/vandadnp
// š Free Flutter Course https://linktr.ee/vandadnp
// š¦ 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// š¶ 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// š¤ Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
debugShowCheckedModeBanner: false,
),
);
}
const images = [
'https://bit.ly/3x7J5Qt',
'https://bit.ly/3ywI8l6',
'https://bit.ly/3jOueGG',
'https://bit.ly/3qYOtDm',
'https://bit.ly/3wt11Ec',
'https://bit.ly/3yvFg7X',
'https://bit.ly/3ywzOla',
'https://bit.ly/3wnASpW',
'https://bit.ly/3jXSDto',
];
class NetworkImage extends StatelessWidget {
final String url;
const NetworkImage({
Key? key,
required this.url,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Image.network(
url,
alignment: Alignment.center,
width: double.infinity,
height: double.infinity,
fit: BoxFit.cover,
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class Animator extends FloatingActionButtonAnimator {
@override
Offset getOffset({
required Offset begin,
required Offset end,
required double progress,
}) {
final x = begin.dx + (end.dx - begin.dx) * progress;
final y = begin.dy + (end.dy - begin.dy) * progress;
return Offset(
x,
y,
);
}
@override
Animation<double> getRotationAnimation({
required Animation<double> parent,
}) {
return CurvedAnimation(
curve: Curves.easeInOutSine,
parent: parent,
);
}
@override
Animation<double> getScaleAnimation({
required Animation<double> parent,
}) {
return CurvedAnimation(
curve: Curves.easeInOutSine,
parent: parent,
);
}
}
class _HomePageState extends State<HomePage> {
late final ScrollController _controller;
var _fabLocation = FloatingActionButtonLocation.endFloat;
@override
void initState() {
super.initState();
_controller = ScrollController();
_controller.addListener(() {
if (_controller.position.atEdge && _controller.position.pixels != 0.0) {
_moveFabToTop();
} else {
_moveFabToBottom();
}
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _moveFabToTop() {
if (_fabLocation != FloatingActionButtonLocation.endTop) {
setState(() {
_fabLocation = FloatingActionButtonLocation.endTop;
});
}
}
void _moveFabToBottom() {
if (_fabLocation != FloatingActionButtonLocation.endFloat) {
setState(() {
_fabLocation = FloatingActionButtonLocation.endFloat;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Floating Action Button',
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.amber,
onPressed: () {},
child: const Icon(
Icons.add,
),
),
floatingActionButtonLocation: _fabLocation,
floatingActionButtonAnimator: Animator(),
body: ImageList(
controller: _controller,
),
);
}
}
class ImageList extends StatelessWidget {
const ImageList({
Key? key,
required ScrollController controller,
}) : _controller = controller,
super(key: key);
final ScrollController _controller;
@override
Widget build(BuildContext context) {
return ListView.builder(
controller: _controller,
itemCount: images.length,
itemBuilder: (context, index) {
final url = images.elementAt(index);
return SizedBox(
height: 200,
child: NetworkImage(
url: url,
),
);
},
);
}
}