user6274128
user6274128

Reputation:

Flutter: How to check if Widget is a scrolling widget

Widgets like ListView, GridView, SingleChildScrollView etc are scrollable widgets while Container, SizedBox, Column are not.

Is there any way to check if the Widget is scrollable, using something like

Widget widget = SomeWidget();
bool scrollable = widget.isScrollable(): // any property like this?

Upvotes: 3

Views: 1979

Answers (1)

Shababb Karim
Shababb Karim

Reputation: 3703

When you write something like this:

Widget widget = SomeWidget();

then you are basically Downcasting your widget to Widget class, which is the parent of all classes. If you check the Widget class in doc you should see only 3 methods exposed. Which are:

  1. createElement
  2. debugFillProperties
  3. toStringShort

bool scrollable = widget.isScrollable(): // any property like this?

So no, this is not possible.

However, you know that the widget has to be a subclass of ScrollView or is of type SingleChildScrollView so you can write a utility method for yourself. Like

bool isScrollable(Widget widget) => widget is ScrollView || widget is SingleChildScrollView;

Upvotes: 1

Related Questions