Flutter - Enable scroll only if content height is greater than screen height
Asked Answered
K

3

9

I am taking ListView() or SingleChildScrollView() in body widget by default. So it is scrolling the content if it is less content. I want to scroll enable only if content is greater than screen height. If content height is less than screen height, need to disable scroll.

double? physicalSizeScreenHeight =
  ui.window.physicalSize.height / ui.window.devicePixelRatio;

physics: physicalSizeScreenHeight <= 700 
? AlwaysScrollableScrollPhysics()
: NeverScrollableScrollPhysics(), 

It is working for some devices and not working for some devices depends on screen width and height and also depends on resolution.

Without checking any condition at "physics" for always or never and allow to AlwaysScrollableScrollPhysics(), then depends on conent height, need to enable/disable scroll.

Any suggestions would be helpful to solve this issue.

Kowtko answered 25/11, 2021 at 13:44 Comment(0)
Q
8

In fact, one of the (many) differences between SingleChildScrollView wrapping a Column, vs directly using a ListView, is the the former will only scroll if there are too many items in the Column.

If that's not what you are seeing, you might have other layout issues.

Try this simple layout SingleChildScrollView > Column > Text('Hi') and verify that it won't scroll.

Quarles answered 25/11, 2021 at 21:6 Comment(0)
C
0

try to use it

final size = MediaQuery.of(context);
final apparentSize = size.size.height - size.padding.bottom - size.padding.top
Cryptoclastic answered 25/11, 2021 at 14:8 Comment(0)
I
0

Check if your content exceeds the screensize using this,

if (_scrollController.position.extentInside != _scrollController.position.extentTotal) { // .. your code here }

Interruption answered 5/1 at 22:7 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lumbago

© 2022 - 2024 — McMap. All rights reserved.