-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
184 lines (167 loc) · 6.21 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
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Scroll Stutter Test',
home: Scaffold(body: FeedView()),
);
}
}
class FeedView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _FeedViewState();
}
}
class _FeedViewState extends State<FeedView> {
final scrollPosNotifier = ValueNotifier<double>(0.0);
ImageProvider _getRandomImage(int index) {
final imageId = 50 + (index) % 50;
return AssetImage("img/$imageId.jpg");
}
String _genRandomText({int seed, int numWords}) {
final random = Random(seed);
return Iterable.generate(
numWords,
(_) => String.fromCharCodes(Iterable.generate(3 + random.nextInt(5),
(_) => (random.nextBool() ? 65 : 97) + random.nextInt(26))))
.join(" ");
}
@override
Widget build(BuildContext context) {
final windowPadding = MediaQuery.of(context).padding;
return LayoutBuilder(builder: (ctx, constraints) {
// Workaround for https://github.com/flutter/flutter/issues/25827
if (constraints.maxHeight == 0) {
return Container();
}
final itemExtent = (constraints.maxHeight - windowPadding.top) * 0.85;
return NotificationListener<ScrollUpdateNotification>(
child: ListView.builder(
padding: windowPadding,
itemExtent: itemExtent,
itemBuilder: (ctx, index) => FeedItem(
image: _getRandomImage(index),
title: _genRandomText(seed: index, numWords: 2),
description: _genRandomText(seed: index, numWords: 10),
scrollPosNotifier: scrollPosNotifier,
visibilityResolver: ItemVisibilityResolver(
itemExtent: itemExtent, index: index),
),
),
onNotification: (notif) {
scrollPosNotifier.value = notif.metrics.pixels;
return true;
});
});
}
}
// Parallax calculation adapted from
// https://iirokrankka.com/2017/09/23/bringing-the-pagetransformer-from-android-to-flutter/
class ItemVisibilityResolver {
final double itemExtent;
final int index;
ItemVisibilityResolver({@required this.itemExtent, @required this.index});
// Returns a value from -1 to 1, depending on the scroll position
double resolveVisibility(double scrollPosition) {
final basePos = itemExtent * index;
return ((basePos - scrollPosition) / itemExtent).clamp(-1.0, 1.0);
}
}
class FeedItem extends StatelessWidget {
final ImageProvider image;
final String title;
final String description;
final ValueNotifier<double> scrollPosNotifier;
final ItemVisibilityResolver visibilityResolver;
FeedItem(
{@required this.image,
@required this.title,
@required this.description,
@required this.scrollPosNotifier,
@required this.visibilityResolver});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: 18.0, right: 18.0, top: 18.0, bottom: 0),
decoration: BoxDecoration(boxShadow: [
BoxShadow(color: Colors.black.withAlpha(0x35), blurRadius: 9.0)
]),
child: ClipRRect(
borderRadius: BorderRadius.circular(9.0),
child: Stack(children: [
ParallaxImage(
image: image,
scrollPosNotifier: scrollPosNotifier,
visibilityResolver: visibilityResolver),
Container(
width: double.infinity,
padding: EdgeInsets.all(24.0),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black.withAlpha(0x80)
])),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title,
style: Theme.of(context)
.textTheme
.display1
.apply(color: Colors.white)),
SizedBox(height: 9.0),
Text(description,
style: Theme.of(context)
.textTheme
.body1
.apply(color: Colors.white))
]))
])));
}
}
class ParallaxImage extends StatelessWidget {
final ImageProvider image;
final ValueNotifier<double> scrollPosNotifier;
final ItemVisibilityResolver visibilityResolver;
static const PARALLAX_EXTENT = 75.0;
ParallaxImage(
{@required this.image,
@required this.scrollPosNotifier,
@required this.visibilityResolver});
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (ctx, constraints) {
final width = constraints.maxWidth + 2; // Add 1px strip on both sides
final height = constraints.maxHeight + 2 * PARALLAX_EXTENT;
return RepaintBoundary(
child: AnimatedBuilder(
animation: scrollPosNotifier,
child: RepaintBoundary(
child: OverflowBox(
minWidth: width,
minHeight: height,
maxHeight: height,
maxWidth: width,
child: Image(
image: image,
fit: BoxFit.cover,
))),
builder: (ctx, child) => Transform.translate(
offset: Offset(
0.0,
-visibilityResolver
.resolveVisibility(scrollPosNotifier.value) *
PARALLAX_EXTENT),
child: child)));
});
}
}