-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
325 lines (302 loc) · 10.1 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:instragram_stories/modals/data.dart';
import 'package:instragram_stories/modals/story_modal.dart';
import 'package:video_player/video_player.dart';
import 'modals/user_modal.dart';
//This is the main method used to make the main app.
//This is the main method used to make the main app.
//For the great things of the past as well as for the future for the great things of the past.
void main() {
runApp(MyApp());
}
//MyApp is the StatelessWidget in the best thing in the world.
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
return MaterialApp(
title: 'Flutter Instagram Stories',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StoryScreen(stories: stories),
);
}
}
class StoryScreen extends StatefulWidget {
final List<Story> stories;
const StoryScreen({
@required this.stories,
});
@override
_StoryScreenState createState() => _StoryScreenState();
}
class _StoryScreenState extends State<StoryScreen>
with SingleTickerProviderStateMixin {
PageController _pageController;
AnimationController _animationController;
VideoPlayerController _videoPlayerController;
int _currentIndex = 0;
@override
void initState() {
super.initState();
_pageController = PageController();
_animationController = AnimationController(duration: new Duration(milliseconds: 1),vsync: this);
final Story firstStory = widget.stories.first;
_loadStory(false, firstStory);
_animationController.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_animationController.stop();
_animationController.reset();
setState(() {
if (_currentIndex + 1 < widget.stories.length) {
_currentIndex += 1;
_loadStory(true, widget.stories[_currentIndex]);
} else {
_currentIndex = 0;
_loadStory(true, widget.stories[_currentIndex]);
}
});
}
});
}
@override
void dispose() {
_pageController.dispose();
_animationController.dispose();
_videoPlayerController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final Story story = widget.stories[_currentIndex];
return Scaffold(
backgroundColor: Colors.black,
body: GestureDetector(
onTapDown: (details) => onTapDown(details, story),
child: Stack(children: [
PageView.builder(
controller: _pageController,
physics: NeverScrollableScrollPhysics(),
itemCount: widget.stories.length,
itemBuilder: (context, i) {
final Story story = widget.stories[i];
switch (story.media) {
case MediaType.image:
return CachedNetworkImage(
imageUrl: story.url,
fit: BoxFit.cover,
);
case MediaType.video:
if (_videoPlayerController != null &&
_videoPlayerController.value.initialized) {
return FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _videoPlayerController.value.size.width,
height: _videoPlayerController.value.size.height,
child: VideoPlayer(_videoPlayerController),
),
);
}
}
return const SizedBox.shrink();
}),
Positioned(
top: 40.0,
left: 10.0,
right: 10.0,
child: Column(
children: <Widget>[
Column(
children: [
Row(
children: widget.stories
.asMap()
.map((i, e) {
return MapEntry(
i,
AnimatedBar(
animController: _animationController,
position: i,
currentIndex: _currentIndex,
),
);
})
.values
.toList(),
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 1.5,
vertical: 10.0,
),
child: UserInfo(user: story.user),
)
],
),
],
),
),
])));
}
void onTapDown(TapDownDetails details, Story story) {
final double screenWidth = MediaQuery.of(context).size.width;
final double dx = details.globalPosition.dx;
if (dx < screenWidth / 3) {
setState(() {
if (_currentIndex - 1 >= 0) {
_currentIndex -= 1;
_loadStory(true, widget.stories[_currentIndex]);
}
});
} else if (dx > 2 * screenWidth / 3) {
setState(() {
if (_currentIndex + 1 < widget.stories.length) {
_currentIndex += 1;
_loadStory(true, widget.stories[_currentIndex]);
} else {
_currentIndex = 0;
_loadStory(true, widget.stories[_currentIndex]);
}
});
} else {
if (story.media == MediaType.video) {
if (_videoPlayerController.value.isPlaying) {
_videoPlayerController.pause();
_animationController.stop();
} else {
_videoPlayerController.play();
_animationController.forward();
}
}
}
}
void _loadStory(bool animateToPage, Story story) {
_animationController.stop();
_animationController.reset();
switch (story.media) {
case MediaType.image:
// ignore: unnecessary_statements
_animationController.duration == story.duration;
_animationController.forward();
break;
case MediaType.video:
_videoPlayerController = null;
_videoPlayerController?.dispose();
_videoPlayerController = VideoPlayerController.network(story.url)
..initialize().then((_) {
setState(() {
if (_videoPlayerController.value.initialized) {
_animationController.duration =
_videoPlayerController.value.duration;
_videoPlayerController.play();
_animationController.forward();
}
});
});
break;
}
if (animateToPage) {
_pageController.animateToPage(_currentIndex,
duration: const Duration(milliseconds: 1), curve: Curves.easeInOut);
}
}
}
class AnimatedBar extends StatelessWidget {
final AnimationController animController;
final int position;
final int currentIndex;
const AnimatedBar({
Key key,
@required this.animController,
@required this.position,
@required this.currentIndex,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 1.5),
child: LayoutBuilder(
builder: (context, constraints) {
return Stack(
children: [
_buildContainer(
double.infinity,
position < currentIndex
? Colors.white
: Colors.white.withOpacity(0.5),
),
position == currentIndex
? AnimatedBuilder(
animation: animController,
builder: (context, child) {
return _buildContainer(
constraints.maxWidth * animController.value,
Colors.white,
);
},
)
: const SizedBox.shrink(),
],
);
},
)));
}
Container _buildContainer(double width, Color color) {
return Container(
height: 5.0,
width: width,
decoration: BoxDecoration(
color: color,
border: Border.all(
color: Colors.black26,
width: 8.0,
),
borderRadius: BorderRadius.circular(3.0),
));
}
}
class UserInfo extends StatelessWidget {
final User user;
const UserInfo({
Key key,
@required this.user,
}) : super(key: key);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Row(children: [
CircleAvatar(
radius: 20.0,
backgroundColor: Colors.grey[300],
backgroundImage: CachedNetworkImageProvider(
user.profileImageUrl,
),
),
const SizedBox(width: 10.0),
Expanded(
child: Text(user.name,
style: const TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w600,
))),
IconButton(
icon: const Icon(
Icons.close,
size: 30.0,
color: Colors.white,
),
onPressed: () => Navigator.of(context).pop(),
),
]);
}
}