Home > Net >  Flutter - Enable scroll only if content height is greater than screen height
Flutter - Enable scroll only if content height is greater than screen height

Time:11-26

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.

CodePudding user response:

try to use it

final size = MediaQuery.of(context);
final apparentSize = size.size.height - size.padding.bottom - size.padding.top

CodePudding user response:

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.

  • Related