-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed_widget.dart
212 lines (194 loc) · 6.91 KB
/
feed_widget.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
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:devour/application/feed/bloc/feed_bloc.dart';
import 'package:devour/injection.dart';
import 'package:devour/presentation/screens/feed/feed_image_widget.dart';
import 'package:devour/presentation/screens/feed/feed_scroll_physics.dart';
import 'package:devour/presentation/screens/feed/feed_sliver_list.dart';
import 'package:devour/presentation/screens/feed/post_widget.dart';
import 'package:devour/presentation/widgets/animations/animated_opacity_reverse.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:fpdart/fpdart.dart' show Option;
import 'package:lottie/lottie.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:collection/collection.dart';
class FeedWidget extends StatefulWidget {
const FeedWidget({Key? key}) : super(key: key);
@override
State<FeedWidget> createState() => _FeedWidgetState();
}
class _FeedWidgetState extends State<FeedWidget> {
late CacheManager cacheManager = serviceLocator<CacheManager>();
final ScrollController controller = ScrollController();
final CurrentElementListener currentElementListener =
CurrentElementListener();
final renderedMemes = <int, Size>{};
final renderedMemesKeys = <int, GlobalKey>{};
@override
void initState() {
super.initState();
currentElementListener.currentIndex.addListener(updateSelectedMeme);
// cacheManager.emptyCache();
}
@override
void dispose() {
currentElementListener.currentIndex.removeListener(updateSelectedMeme);
super.dispose();
}
void updateSelectedMeme() {
final bloc = BlocProvider.of<FeedBloc>(context);
bloc.add(
FeedEvent.select(
currentElementListener.currentIndex.value,
Option.fromNullable(
renderedMemesKeys[currentElementListener.currentIndex.value],
),
),
);
}
@override
Widget build(BuildContext context) {
return BlocBuilder<FeedBloc, FeedState>(
builder: (context, state) {
Widget getContent(BuildContext context, FeedState state) {
if (state.isLoading) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: Center(
child: Lottie.asset('lib/assets/animations/space_loader.json'),
),
);
}
Widget buildSliverList(double maxHeight, BoxConstraints cnstr) {
return CustomScrollView(
physics: FeedScrollPhysics(
renderedMemes,
maxHeight,
topPadding: MediaQuery.of(context).padding.top,
),
slivers: [
FeedSliverList(
listener: currentElementListener,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
final imageProvider = CachedNetworkImageProvider(
state.memes[index].imageLink,
cacheManager: cacheManager,
);
final key = renderedMemesKeys.putIfAbsent(
index,
() => GlobalKey(),
);
if (renderedMemes[index] == null) {
propogateSizeToMap(
imageProvider,
cnstr,
maxHeight,
index,
);
renderedMemes[index] = Size(cnstr.maxWidth, 200);
}
return FeedImage(
key: key,
imageProvider: imageProvider,
constraints: cnstr,
onRefreshPressed: () => setState(() {
renderedMemesKeys[index] = GlobalKey();
}),
);
}),
)
],
);
}
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints cnstr) {
// We can use safe area here, but it will create more problems
// UPD: What promlems?
final maxHeight = cnstr.maxHeight -
(MediaQuery.of(context).padding.bottom +
MediaQuery.of(context).padding.top);
return Stack(
children: [
buildSliverList(maxHeight, cnstr),
buildVignette(),
// Build overlay with actions, description and listenable of current
AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: PostWidget(
state,
constraints: cnstr,
key: ValueKey(state.currentMemeModel.sourceLink),
),
)
],
);
},
);
}
return AnimatedSwitcher(
duration: const Duration(milliseconds: 500),
child: Container(
key: Key(state.isLoading.toString()),
child: getContent(context, state),
),
);
},
);
}
IgnorePointer buildVignette() {
return IgnorePointer(
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(0, 0, 0, 0.3),
Color.fromRGBO(0, 0, 0, 0.0),
Color.fromRGBO(0, 0, 0, 0.0),
Color.fromRGBO(0, 0, 0, 0.3),
],
),
),
),
);
}
void propogateSizeToMap(
CachedNetworkImageProvider imageProvider,
BoxConstraints constraints,
double maxHeight,
int index,
) {
late ImageStreamListener listener;
listener = ImageStreamListener((info, _) {
// Calculating correct size of resulting image
Size size = Size(
info.image.width.toDouble(),
info.image.height.toDouble(),
);
size = Size(
constraints.maxWidth,
constraints.maxWidth / size.aspectRatio,
);
// For updating UI after fetching image (without it, overlayed image will be small)
WidgetsBinding.instance!.addPostFrameCallback(
(_) => setState(() {
renderedMemes[index] = size;
print('index $index is $size');
imageProvider
.resolve(ImageConfiguration.empty)
.removeListener(listener);
}),
);
});
imageProvider.resolve(ImageConfiguration.empty).addListener(
listener,
);
}
}