Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to make a ListView transparent to pointer events (but not its non-transparent contents)?

I have a Scrollable (ListView or any other) and it contains a transparent widget, say Container(height:200). I can see through both the widget and the scrollable (in other words I can see the widgets behind the scrollable).

How can I be able to click through the transparent widget and the scrollable, so that I reach the widgets behind the scrollable?

ListView(
  children: [
    Container(height: 200), // Transparent.
    Container(color: Colors.red, height: 200),
    ],
);

Note, I cannot wrap the scrollable with IgnorePointer, because I still want to click the non-transparent widgets in the scrollable.

like image 287
MarcG Avatar asked Aug 19 '19 23:08

MarcG


People also ask

How to make any widget transparent in flutter?

See the example Below: You need to wrap widget with Opacity () widget and set opacity paramater with transparent value. In this way you can make any widget transparent by appling opacity in Flutter widget. In this example, we are going to show you the easiest way to make any widget clickable.

How to create listview in flutter?

ListView example Step 1: Create a new flutter project Go to the VSCode and hit the shortcut keys: cmd + shift + p and type the Flutter,... Step 2: Write the Stateful Widget Now, if you do not know how to write the Stateful Widget in Flutter, then check out my... Step 3: Add ListView.builder () ...

How to display a user’s profile image when absent in flutter?

ListView is normally used to display avatars beside each item. Flutter has a CircleAvatar widget to display a user’s profile image, or initials when absent. In the leading prop we add the CircleAvatar widget, so the ListView starts with the image. The backgroundImage prop in the CircleAvatar sets the background image of the widget.

What happens if we don’t use listview?

If we do not use ListView, it will throw a warning in a yellow line to indicate that we need to use some widget to show the proper user content, and that is why we will use the ListView widget Flutter. Flutter provides ListView.builder which can be used to generate dynamic content from external sources. There are four types of ListViews.


1 Answers

The only reasonable solution I can think of is to have a GestureDetector on the transparent container, which will give you the global position of the taps:

GestureDetector(
  onTapUp: (TapUpDetails tapUpDetails) {
    print("onTapUp global: " + tapUpDetails.globalPosition.toString());
  },

And then add a Key to the widget behind the list, and use it to get the global position of the top left corner of the widget's rectangle, as well as the size of the widget, and use those to get the rectangle of the widget:

RenderBox renderBox = _key.currentContext.findRenderObject();
Offset topLeftCorner = renderBox.localToGlobal(Offset.zero);
Size size = renderBox.size;
Rect rectangle = topLeftCorner & size;

If the background widget does not move, you can do it within initState on the very next frame using WidgetsBinding.instance.addPostFrameCallback (the render object will be null if do it synchronously within initState) and save the Rect - otherwise you will have to recalculate it on every tap.

And then finally on each tap on the transparent container you can calculate whether the tap's position is within the boundaries of the background widget, and invoke the corresponding code:

// if tap is within boundaries of the background widget
if (rectangle.contains(tapUpDetails.globalPosition)) {
  // your code here
}
like image 102
Ovidiu Avatar answered Sep 29 '22 17:09

Ovidiu