Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENV-1444: Fix gradient rendering at scroll edges in ScrollGradientMask #1419

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 79 additions & 40 deletions lib/ui/components/linear_gradient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
import 'package:envoy/ui/theme/envoy_colors.dart';
import 'package:flutter/material.dart';

class LinearGradients {
static LinearGradient blogPostGradient(double topGradientEnd) {
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: const [
EnvoyColors.solidWhite,
Colors.transparent,
Colors.transparent,
EnvoyColors.solidWhite,
],
stops: [0.0, topGradientEnd, 0.85, 0.96],
);
}
}

class ScrollGradientMask extends StatefulWidget {
final Widget child;
final double start;
final double topGradientValue;
final double bottomGradientValue;
final double end;

const ScrollGradientMask({required this.child, super.key});
const ScrollGradientMask({
required this.child,
this.start = 0.0,
this.topGradientValue = 0.05,
this.bottomGradientValue = 0.95,
this.end = 1.0,
super.key,
});

@override
ScrollGradientMaskState createState() => ScrollGradientMaskState();
Expand All @@ -33,50 +28,94 @@ class ScrollGradientMask extends StatefulWidget {
class ScrollGradientMaskState extends State<ScrollGradientMask> {
final ValueNotifier<double> topGradientEndNotifier =
ValueNotifier<double>(0.0);
final ValueNotifier<double> bottomGradientStartNotifier =
ValueNotifier<double>(1.0);
late ScrollController _scrollController;

@override
void initState() {
super.initState();
_scrollController = ScrollController()
..addListener(() {
if (_scrollController.offset == 0) {
topGradientEndNotifier.value = 0.0;
} else {
topGradientEndNotifier.value = 0.05;
}
_updateGradients();
});

topGradientEndNotifier.value = widget.start;
bottomGradientStartNotifier.value = widget.bottomGradientValue;
}

void _updateGradients() {
if (_scrollController.offset == 0) {
topGradientEndNotifier.value = widget.start;
bottomGradientStartNotifier.value = widget.bottomGradientValue;
} else if (_scrollController.offset >=
_scrollController.position.maxScrollExtent) {
topGradientEndNotifier.value = widget.topGradientValue;
bottomGradientStartNotifier.value = widget.end;
} else {
topGradientEndNotifier.value = widget.topGradientValue;
bottomGradientStartNotifier.value = widget.bottomGradientValue;
}
}

@override
void dispose() {
_scrollController.dispose();
topGradientEndNotifier.dispose();
bottomGradientStartNotifier.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return ValueListenableBuilder<double>(
valueListenable: topGradientEndNotifier,
builder: (context, topGradientEnd, child) {
return ShaderMask(
shaderCallback: (Rect rect) {
return LinearGradients.blogPostGradient(topGradientEnd)
.createShader(rect);
valueListenable: bottomGradientStartNotifier,
builder: (context, bottomGradientStart, child) {
return ValueListenableBuilder<double>(
valueListenable: topGradientEndNotifier,
builder: (context, topGradientEnd, child) {
return ShaderMask(
shaderCallback: (Rect rect) {
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: const [
EnvoyColors.solidWhite,
Colors.transparent,
Colors.transparent,
EnvoyColors.solidWhite,
],
stops: [
0.0,
topGradientEnd,
bottomGradientStart,
1.0,
],
).createShader(rect);
},
blendMode: BlendMode.dstOut,
child: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification.metrics.pixels == 0) {
topGradientEndNotifier.value = widget.start;
bottomGradientStartNotifier.value =
widget.bottomGradientValue;
} else if (scrollNotification.metrics.pixels >=
scrollNotification.metrics.maxScrollExtent) {
topGradientEndNotifier.value = widget.topGradientValue;
bottomGradientStartNotifier.value = widget.end;
} else {
topGradientEndNotifier.value = widget.topGradientValue;
bottomGradientStartNotifier.value =
widget.bottomGradientValue;
}
return true;
},
child: widget.child,
),
);
},
blendMode: BlendMode.dstOut,
child: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification.metrics.pixels == 0) {
topGradientEndNotifier.value = 0.0;
} else {
topGradientEndNotifier.value = 0.05;
}
return true;
},
child: widget.child,
),
child: widget.child,
);
},
child: widget.child,
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/home/cards/learn/components/blog_post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class BlogPostCardState extends State<BlogPostCard> {
Widget build(BuildContext context) {
return Center(
child: ScrollGradientMask(
bottomGradientValue: 0.85,
end: 0.96,
child: Padding(
padding: const EdgeInsets.only(
bottom: EnvoySpacing.large1,
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/home/cards/learn/learn_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class _LearnCardState extends ConsumerState<LearnCard> {
final bool isAllEmpty = isSearchEmpty || isFilterEmpty;

return ScrollGradientMask(
bottomGradientValue: 0.85,
end: 0.96,
child: CustomScrollView(
physics: isAllEmpty ? const NeverScrollableScrollPhysics() : null,
slivers: [
Expand Down
Loading