I am building an app where you can browse various products. Each product also has a product detail page. On that product detail page, I want to display the product image, the price, a product description and also a comment section.
Because the product description can be quite long, I don’t want to show the entire description right away to the user, but rather enable the user to be able to expand the description with a click on read more.
Below my product description is a comments section. The comments can expand to the top as well as to the bottom. The reason behind this is it is basically like a paginated site where you can load older comments and newer comments. I have solved this as described here.
However, because I have center key set, the product description does expand to the top when I click read more. That’s the behavior I want to change.
- I want my product description to expand to the bottom and push all widgets below down
- Older comments to expand to the top and push the all widgets above up
- Newer comments to expand to the bottom and push all widgets below down
At all time, the user should stay where he is on the screen. I do not want to use functions like jumpTo
to jump to the top when the product description expands up. I want to solve this problem properly and have the product description expanding down. How can I archive that?
Secondly, I also want to archive that when a user opens the page it starts at the top, with the product image, like every other page. What do I need to do in order to archive that goal too?
Here you can find an example of my code. I have added two floating buttons which simulate comments being added to the top and to the bottom: https://dartpad.dev/?id=486578f48833dd3d53b1f76080ac6f23
I am grateful for any kind of help!
2
Answers
To achieve the desired behavior where the product description expands downwards and pushes the content below it, and where the comments expand both upwards and downwards without jumping the user to the top of the page, you can follow these steps:
Here’s how you can modify your code to achieve these goals:
I have completed the necessary work on your case and have devised a final solution.
The current code utilizes the
center
key for theCustomScrollView
, causing the upper half to move upwards and the lower half to move downwards. This conflicts with the requirement for the product description to expand downwards.Therefore, I propose removing the
center
key and implementing a customScrollPhysics
class to maintain the position of theCustomScrollView
.You must manually determine when to retain the scroll and when not to. The system cannot automatically recognize what is appropriate for your specific case. Please refer to the
_shouldRetainScroll
usage for more information.Also, with the removal of the
center
key, the page will now start at the top when a user opens it.Finally, the solution may not be perfect, but give it a go. I look forward to your response.
Below is the implemented code: