-
Notifications
You must be signed in to change notification settings - Fork 63
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
SliverWithPinnedFooter #59
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/rendering.dart'; | ||
|
||
class RenderSliverWithPinnedFooterParentData extends ParentData | ||
with ContainerParentDataMixin<RenderObject> {} | ||
|
||
class RenderSliverWithPinnedFooter extends RenderSliver | ||
with | ||
ContainerRenderObjectMixin<RenderObject, | ||
RenderSliverWithPinnedFooterParentData> { | ||
@override | ||
void setupParentData(covariant RenderObject child) { | ||
if (child.parentData is! RenderSliverWithPinnedFooterParentData) { | ||
child.parentData = RenderSliverWithPinnedFooterParentData(); | ||
} | ||
} | ||
|
||
@override | ||
void applyPaintTransform(covariant RenderObject child, Matrix4 transform) { | ||
if (child == lastChild) { | ||
final geometry = this.geometry!; | ||
final footer = child as RenderBox; | ||
final footerTop = | ||
geometry.paintOrigin + geometry.paintExtent - footer.size.height; | ||
transform.translate(0.0, footerTop); | ||
} | ||
} | ||
|
||
@override | ||
void performLayout() { | ||
final sliver = firstChild as RenderSliver; | ||
final footer = lastChild as RenderBox; | ||
sliver.layout(constraints, parentUsesSize: true); | ||
footer.layout(constraints.asBoxConstraints(), parentUsesSize: true); | ||
final paintExtent = min(constraints.remainingPaintExtent, | ||
sliver.geometry!.paintExtent + footer.size.height); | ||
final layoutExtent = min(constraints.remainingPaintExtent, | ||
sliver.geometry!.layoutExtent + footer.size.height); | ||
final cacheExtent = min(constraints.remainingPaintExtent, | ||
sliver.geometry!.cacheExtent + footer.size.height); | ||
geometry = SliverGeometry( | ||
scrollExtent: sliver.geometry!.scrollExtent + footer.size.height, | ||
paintExtent: paintExtent, | ||
paintOrigin: sliver.geometry!.paintOrigin, | ||
layoutExtent: layoutExtent, | ||
maxPaintExtent: sliver.geometry!.maxPaintExtent + footer.size.height, | ||
maxScrollObstructionExtent: sliver.geometry!.maxScrollObstructionExtent, | ||
hitTestExtent: paintExtent, | ||
visible: sliver.geometry!.visible, | ||
hasVisualOverflow: sliver.geometry!.hasVisualOverflow, | ||
scrollOffsetCorrection: sliver.geometry!.scrollOffsetCorrection, | ||
cacheExtent: cacheExtent, | ||
); | ||
} | ||
|
||
@override | ||
bool hitTestChildren( | ||
SliverHitTestResult result, { | ||
double? mainAxisPosition, | ||
double? crossAxisPosition, | ||
}) { | ||
if (mainAxisPosition == null || crossAxisPosition == null) return false; | ||
|
||
final geometry = this.geometry!; | ||
final sliver = firstChild as RenderSliver; | ||
final footer = lastChild as RenderBox; | ||
final footerTop = | ||
geometry.paintOrigin + geometry.paintExtent - footer.size.height; | ||
if (mainAxisPosition >= footerTop) { | ||
final hit = footer.hitTest( | ||
BoxHitTestResult.wrap(result), | ||
position: Offset(crossAxisPosition, mainAxisPosition - footerTop), | ||
); | ||
if (hit) { | ||
return true; | ||
} | ||
} | ||
return sliver.hitTest( | ||
result, | ||
mainAxisPosition: mainAxisPosition, | ||
crossAxisPosition: crossAxisPosition, | ||
); | ||
} | ||
|
||
@override | ||
void paint(PaintingContext context, Offset offset) { | ||
final geometry = this.geometry!; | ||
final sliver = firstChild as RenderSliver; | ||
final footer = lastChild as RenderBox; | ||
context.paintChild(sliver, offset); | ||
context.paintChild( | ||
footer, | ||
Offset( | ||
offset.dx, | ||
offset.dy + | ||
geometry.paintOrigin + | ||
geometry.paintExtent - | ||
footer.size.height, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'rendering/sliver_with_pinned_footer.dart'; | ||
|
||
/// [SliverWithPinnedFooter] adds the ability to have a pinned footer at the bottom of a sliver. | ||
/// | ||
/// The sliver will be layed out first and the footer will be added below if there | ||
/// is enough room left in the viewport. If there is not enough room then the footer | ||
/// will be painted on top of the sliver at the bottom of the painted area of the sliver. | ||
/// | ||
/// The total size of this sliver will be the paintExtent of the passed in sliver | ||
/// plus the height of the footer. | ||
class SliverWithPinnedFooter extends MultiChildRenderObjectWidget { | ||
SliverWithPinnedFooter({ | ||
Key? key, | ||
required Widget sliver, | ||
required Widget footer, | ||
}) : super(key: key, children: [sliver, footer]); | ||
|
||
@override | ||
RenderObject createRenderObject(BuildContext context) { | ||
return RenderSliverWithPinnedFooter(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fix the footer appearing first, line 99 should be replaced with
min(footer.size.height, geometry.paintExtent)
(same for the hitTest)