-
Notifications
You must be signed in to change notification settings - Fork 0
/
y_shadow_scroll_container.dart
91 lines (82 loc) · 2.83 KB
/
y_shadow_scroll_container.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
part of components;
@Deprecated('Just use a list instead')
class YShadowScrollContainer extends StatefulWidget {
final List<Widget> children;
final Color color;
final bool shrinkWrap;
const YShadowScrollContainer({Key? key, required this.children, required this.color, this.shrinkWrap = false})
: super(key: key);
@override
_YShadowScrollContainerState createState() => _YShadowScrollContainerState();
}
// ignore: deprecated_member_use_from_same_package
class _YShadowScrollContainerState extends State<YShadowScrollContainer> {
final ScrollController _scrollController = ScrollController();
late bool showTopGradient = false;
late bool showBottomGradient = false;
final double distance = 5;
final double gradientHeight = YScale.s8;
final Duration duration = const Duration(milliseconds: 250);
final Curve curve = Curves.easeInOut;
void setGradientsVisibility() {
setState(() {
showTopGradient = _scrollController.offset > distance;
showBottomGradient = _scrollController.offset < (_scrollController.position.maxScrollExtent - distance);
});
}
@override
void initState() {
super.initState();
_scrollController.addListener(setGradientsVisibility);
WidgetsBinding.instance!.addPostFrameCallback((_) => setGradientsVisibility());
}
@override
void dispose() {
_scrollController.dispose(); // it is a good practice to dispose the controller
super.dispose();
}
Widget _gradient(BuildContext context, bool isTop) {
return Container(
height: gradientHeight,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: isTop ? Alignment.topCenter : Alignment.bottomCenter,
end: !isTop ? Alignment.topCenter : Alignment.bottomCenter,
colors: [widget.color, widget.color.withOpacity(0)],
)),
);
}
@override
Widget build(BuildContext context) {
return Stack(children: [
ListView(
controller: _scrollController,
children: widget.children,
shrinkWrap: widget.shrinkWrap,
physics: widget.shrinkWrap ? const ClampingScrollPhysics() : null,
),
Positioned(
top: -1,
left: 0,
right: 0,
child: IgnorePointer(
ignoring: !showTopGradient,
child: AnimatedOpacity(
curve: curve,
opacity: showTopGradient ? 1 : 0,
duration: duration,
child: _gradient(context, true)))),
Positioned(
bottom: -1,
left: 0,
right: 0,
child: IgnorePointer(
ignoring: !showBottomGradient,
child: AnimatedOpacity(
curve: curve,
opacity: showBottomGradient ? 1 : 0,
duration: duration,
child: _gradient(context, false)))),
]);
}
}