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

Support non-opaque AppBar background colors #2

Merged
merged 2 commits into from
May 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 22 additions & 24 deletions lib/src/app_bar/bottom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,48 @@ class AnimatedBottom extends AnimatedAppBarPart implements PreferredSizeWidget {
final childStart = hasParent ? 0.5 : 0;
return SizedBox(
height: preferredHeight,
child: Stack(
children: <Widget>[
if (hasParent && t < parentEnd)
Positioned.fill(top: null, child: parent.appBar.bottom),
if (hasChild && t > childStart)
Positioned.fill(top: null, child: child.appBar.bottom),
Positioned.fill(
child: DecoratedBox(
decoration: BoxDecoration(
gradient: _buildScrimGradient(
hasParent: hasParent,
hasChild: hasChild,
),
),
),
),
],
child: ShaderMask(
shaderCallback: (rect) => _buildScrimShader(
rect,
hasParent: hasParent,
hasChild: hasChild,
),
blendMode: BlendMode.dstOut,
child: Stack(
children: <Widget>[
if (hasParent && t < parentEnd)
Positioned.fill(top: null, child: parent.appBar.bottom),
if (hasChild && t > childStart)
Positioned.fill(top: null, child: child.appBar.bottom),
],
),
),
);
}

Gradient _buildScrimGradient({
Shader _buildScrimShader(
Rect rect, {
@required bool hasParent,
@required bool hasChild,
}) {
final triangleT = math.min(t, 1 - t) * 2;
final backgroundColor = state.backgroundColor;

return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
if (hasParent && hasChild)
backgroundColor.withOpacity(triangleT)
Colors.white.withOpacity(triangleT)
else if (hasParent)
backgroundColor.withOpacity(t)
Colors.white.withOpacity(t)
else if (hasChild)
backgroundColor.withOpacity(1 - t),
backgroundColor.withAlpha(0),
Colors.white.withOpacity(1 - t),
Colors.white.withAlpha(0),
],
stops: [
if (hasParent && hasChild) triangleT else 0.0,
1,
],
);
).createShader(rect);
}
}
8 changes: 2 additions & 6 deletions lib/src/app_bar/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ class MorphingState {
final EndState child;
final double t;

Color get backgroundColor {
assert(parent.backgroundColor.isOpaque);
assert(child.backgroundColor.isOpaque);

return Color.lerp(parent.backgroundColor, child.backgroundColor, t);
}
Color get backgroundColor =>
Color.lerp(parent.backgroundColor, child.backgroundColor, t);
}

@immutable
Expand Down