Non-center alignment for PageView with viewportFraction < 1.0

Multi tool use
When you create a PageView with a viewportFraction value of <1.0 for the PageController, the pages are snapped to the center of the viewport, like so:
I want the current pages to be snapped to the top of the viewport, while the next page is rendered under the bottom bar. I've tried applying a transform to each page:
Transform.translate(offset: Offset(0.0, -36.0), child: page)
This appears to work at first glance (see first image), but fails to render the next page when it would be hidden without the transform (see second image):
I've thought of using a ListView with PageScrollPhysics (which does behave correctly), but then I lose the pagination APIs (the ability to see which page I'm on/jump to a page). What would be the appropriate way to do this, in a way that always renders the next page when it's on-screen?

add a comment |
When you create a PageView with a viewportFraction value of <1.0 for the PageController, the pages are snapped to the center of the viewport, like so:
I want the current pages to be snapped to the top of the viewport, while the next page is rendered under the bottom bar. I've tried applying a transform to each page:
Transform.translate(offset: Offset(0.0, -36.0), child: page)
This appears to work at first glance (see first image), but fails to render the next page when it would be hidden without the transform (see second image):
I've thought of using a ListView with PageScrollPhysics (which does behave correctly), but then I lose the pagination APIs (the ability to see which page I'm on/jump to a page). What would be the appropriate way to do this, in a way that always renders the next page when it's on-screen?

1
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04
add a comment |
When you create a PageView with a viewportFraction value of <1.0 for the PageController, the pages are snapped to the center of the viewport, like so:
I want the current pages to be snapped to the top of the viewport, while the next page is rendered under the bottom bar. I've tried applying a transform to each page:
Transform.translate(offset: Offset(0.0, -36.0), child: page)
This appears to work at first glance (see first image), but fails to render the next page when it would be hidden without the transform (see second image):
I've thought of using a ListView with PageScrollPhysics (which does behave correctly), but then I lose the pagination APIs (the ability to see which page I'm on/jump to a page). What would be the appropriate way to do this, in a way that always renders the next page when it's on-screen?

When you create a PageView with a viewportFraction value of <1.0 for the PageController, the pages are snapped to the center of the viewport, like so:
I want the current pages to be snapped to the top of the viewport, while the next page is rendered under the bottom bar. I've tried applying a transform to each page:
Transform.translate(offset: Offset(0.0, -36.0), child: page)
This appears to work at first glance (see first image), but fails to render the next page when it would be hidden without the transform (see second image):
I've thought of using a ListView with PageScrollPhysics (which does behave correctly), but then I lose the pagination APIs (the ability to see which page I'm on/jump to a page). What would be the appropriate way to do this, in a way that always renders the next page when it's on-screen?


asked Dec 27 '18 at 16:32


yunyu
492513
492513
1
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04
add a comment |
1
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04
1
1
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04
add a comment |
1 Answer
1
active
oldest
votes
I'm actually trying to achieve something similar, I did ask a question here: Is there a way to disable PageView clipping effect?
But thanks to you, your thought of approaching with ListView & PageScrollPhysics is just brilliant, I can solve it now. For calculating the page, you just need to do this:
ScrollController controller = ScrollController();
controller.addListener((){
int page = (controller.offset / viewportWidth).round();
});
// which "viewportWidth" is the width of your page item.
or if you want to animate to a specific page, do this:
int page = 3;
controller.animateTo(
page.toDouble() * viewportWidth,
duration: Duration(milliseconds: 300),
curve: Curves.ease,
);
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948087%2fnon-center-alignment-for-pageview-with-viewportfraction-1-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm actually trying to achieve something similar, I did ask a question here: Is there a way to disable PageView clipping effect?
But thanks to you, your thought of approaching with ListView & PageScrollPhysics is just brilliant, I can solve it now. For calculating the page, you just need to do this:
ScrollController controller = ScrollController();
controller.addListener((){
int page = (controller.offset / viewportWidth).round();
});
// which "viewportWidth" is the width of your page item.
or if you want to animate to a specific page, do this:
int page = 3;
controller.animateTo(
page.toDouble() * viewportWidth,
duration: Duration(milliseconds: 300),
curve: Curves.ease,
);
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
add a comment |
I'm actually trying to achieve something similar, I did ask a question here: Is there a way to disable PageView clipping effect?
But thanks to you, your thought of approaching with ListView & PageScrollPhysics is just brilliant, I can solve it now. For calculating the page, you just need to do this:
ScrollController controller = ScrollController();
controller.addListener((){
int page = (controller.offset / viewportWidth).round();
});
// which "viewportWidth" is the width of your page item.
or if you want to animate to a specific page, do this:
int page = 3;
controller.animateTo(
page.toDouble() * viewportWidth,
duration: Duration(milliseconds: 300),
curve: Curves.ease,
);
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
add a comment |
I'm actually trying to achieve something similar, I did ask a question here: Is there a way to disable PageView clipping effect?
But thanks to you, your thought of approaching with ListView & PageScrollPhysics is just brilliant, I can solve it now. For calculating the page, you just need to do this:
ScrollController controller = ScrollController();
controller.addListener((){
int page = (controller.offset / viewportWidth).round();
});
// which "viewportWidth" is the width of your page item.
or if you want to animate to a specific page, do this:
int page = 3;
controller.animateTo(
page.toDouble() * viewportWidth,
duration: Duration(milliseconds: 300),
curve: Curves.ease,
);
I'm actually trying to achieve something similar, I did ask a question here: Is there a way to disable PageView clipping effect?
But thanks to you, your thought of approaching with ListView & PageScrollPhysics is just brilliant, I can solve it now. For calculating the page, you just need to do this:
ScrollController controller = ScrollController();
controller.addListener((){
int page = (controller.offset / viewportWidth).round();
});
// which "viewportWidth" is the width of your page item.
or if you want to animate to a specific page, do this:
int page = 3;
controller.animateTo(
page.toDouble() * viewportWidth,
duration: Duration(milliseconds: 300),
curve: Curves.ease,
);
answered Dec 28 '18 at 4:46
Goon Nguyen
951513
951513
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
add a comment |
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
Turns out the physics are slightly off when the actual viewportWidth is less than the widget width (at the end, it tries to snap to something beyond bounds), but this math makes it possible to write the physics. I just made a copy of PageScrollPhysics and changed all the position.viewportWidth references to an itemExtent passed in the constructor, while clamping the pixels to position.maxScrollExtent.
– yunyu
Dec 28 '18 at 15:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948087%2fnon-center-alignment-for-pageview-with-viewportfraction-1-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2P00wn,nGNg3WzgB,nH Q0ceWpCLj USogPSXhegSoPzQConnLfAXfrU4cCqyCYdysfr8 8Z91K3YTlnnE wqsT1o,Vne2qYS6je1hOj UY
1
Just a suggestion, completely unverified or tested, but could you go with ListView and use the scroll offset divided by the page height to determine the current page? stackoverflow.com/questions/43881103/…
– Stephen
Dec 27 '18 at 16:43
Hmm, that could work. Let me try that.
– yunyu
Dec 27 '18 at 17:04