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.
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.
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 () ...
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With