59466

In Flutter, how can a positioned Widget feel taps outside of its parent Stack area?

Question:

A Stack contains MyWidget inside of a Positioned.

Stack( overflow: Overflow.visible, children: [ Positioned( top: 0.0, left: 0.0, child: MyWidget(), )], );

Since overflow is Overflow.visible and MyWidget is larger than the Stack, it displays outside of the Stack, which is what I want.

However, I can't tap in the area of MyWidget which is outside of the Stack area. It simply ignores the tap there.

How can I make sure MyWidget accepts gestures there?

Answer1:

Ok, I did a workaround about this, basically I added a GestureDetector on the parent and implemented the onTapDown. Also you have to keep track your Widget using GlobalKey to get the current position.

When the Tap at the parent level is detected check if the tap position is inside your widget.

The code below:

final GlobalKey key = new GlobalKey(); void onTapDown(BuildContext context, TapDownDetails details) { final RenderBox box = context.findRenderObject(); final Offset localOffset = box.globalToLocal(details.globalPosition); final RenderBox containerBox = key.currentContext.findRenderObject(); final Offset containerOffset = containerBox.localToGlobal(localOffset); final onTap = containerBox.paintBounds.contains(containerOffset); if (onTap){ print("DO YOUR STUFF..."); } } @override Widget build(BuildContext context) { return GestureDetector( onTapDown: (TapDownDetails details) => onTapDown(context, details), child: Container( color: Colors.red, width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, child: Align( alignment: Alignment.topLeft, child: SizedBox( width: 200.0, height: 400.0, child: Container( color: Colors.black, child: Stack( overflow: Overflow.visible, children: [ Positioned( top: 0.0, left: 0.0, child: Container( key: key, width: 500.0, height: 200.0, color: Colors.blue, ), ), ], ), ), ), ), ), ); }

Answer2:

I had a similar issue. Basically since the stack's children don't use the fully overflown box size for their hit testing, i used a nested stack and an arbitrary big height so that i can capture the clicks of the nested stack's overflown boxes. Not sure if it can work for you but here goes nothing :)

So in your example maybe you could try something like that

Stack( overflow: Overflow.visible, children: [ Positioned( top: 0.0, left: 0.0, height : 500.0 // biggest possible child size or just very big child: Stack( children: [MyWidget()] ), )], );

Recommend

  • Wordpress Create Category AJAX Response
  • How to create a fast loading wrapping ListBox?
  • ASP Net Core - Mixing External Identity Provider with Individual User Accounts for Audit Tracking
  • Replace rows by index
  • Change color of row programmatically in WatchKit
  • HikariPool-1 - Unusual system clock change detected, soft-evicting connections from pool
  • Recommended way to remove events on destroy with jQuery UI Widget Factory
  • Display forms choice in template-Django
  • Android - HorizontalScrollView will not scroll all the way
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • Stitching 2 images (OpenCV)
  • How to change placeholder text in an autocomplete activity of android google place?
  • Rodeo UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal
  • Two Tables Serving as one Model in Rails
  • Android distinguish between tap and double tap
  • CSS bleed-through with cfinput type=“datefield”
  • Possible to get mouse events fired when cursor is outside page?
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • JqueryMobile Popup menu is not working
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • GridView breaks while scrolling
  • Blackberry - Custom EditField Cursor
  • QLPreviewController hide print button in ios6
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Body moving without any force applied? (Box2d)
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Check if a string to interpolate provides expected placeholders
  • Redux, normalised entities and lodash merge
  • Cannot Parse HTML Data Using Android / JSOUP
  • Android Studio and gradle
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • java string with new operator and a literal