Non-center alignment for PageView with viewportFraction < 1.0

Multi tool use
Multi tool use












1














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:



enter image description here



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):



enter image description hereenter image description here



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?










share|improve this question


















  • 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














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:



enter image description here



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):



enter image description hereenter image description here



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?










share|improve this question


















  • 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








1







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:



enter image description here



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):



enter image description hereenter image description here



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?










share|improve this question













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:



enter image description here



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):



enter image description hereenter image description here



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?







flutter flutter-layout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












1 Answer
1






active

oldest

votes


















1














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,
);





share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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,
);





share|improve this answer





















  • 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
















1














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,
);





share|improve this answer





















  • 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














1












1








1






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,
);





share|improve this answer












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,
);






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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
PqNrNx3Qy6XAvpicSZNdx6bJ,74glzblZJ HK,oKIMYv8w e

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas