MaheshPeri19
MaheshPeri19

Reputation: 402

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

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.

Upvotes: 9

Views: 8859

Answers (3)

HashRay
HashRay

Reputation: 1

Check if your content exceeds the screensize using this,

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

Upvotes: 0

WSBT
WSBT

Reputation: 36323

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.

Upvotes: 8

novol
novol

Reputation: 852

try to use it

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

Upvotes: 0

Related Questions