an alternative to overlay which allows you to easily render and hit test a widget outside its parent bounds.
based on the original idea by @shrouxm here: https://github.com/flutter/flutter/issues/75747
typically in flutter, if you offset a widget outside of it’s parent bounds hit-testing will break. deferpointer
works around this issue by handing off hit-testing and (optionally) rendering to an deferredpointerhandler
widget further up the tree.
while overlay
can solve this issue to some degree, using deferpointer
offers some benefits:
- just works: no error prone and tedious layer management
- more granular: you can set the bounds to be any ancestor widget you choose
- more flexible: you can choose to paint the child on top, or not
- easier to align/pin to a widget in the tree as that is the default expectation
this is useful for larger ui components like dropdown menus and sliding panels, as well as just small general styling tweaks.
�� installation
dependencies:
defer_pointer: ^0.0.2
⚙ import
import 'package:defer_pointer/defer_pointer.dart';
��️ usage
- wrap a
deferredpointerhandler
somewhere above the buttons that you wish to hit-test. - wrap
deferpointer
around the buttons themselves.
widget build(buildcontext context) {
return deferredpointerhandler(
child: sizedbox(
width: 100,
height: 100,
child: stack(clipbehavior: clip.none, children: [
// hang button off the bottom of the content
positioned(
bottom: -30,
child: deferpointer(child: _somebtn(false)),
),
// content
positioned.fill(
child: container(
decoration: boxdecoration(color: colors.green, boxshadow: [
boxshadow(color: colors.black.withopacity(1), blurradius: 4, spreadradius: 4),
]),
),
),
]))));
}
enable paintontop
if you need the child widget painted on top of it’s siblings. this will defer painting to the currently linked deferredpointerhandler
.
return deferpointer(
paintontop: true,
child: textbutton(...));
examples
there are 4 examples in this repo:
-
a simple example of offsetting 2 buttons outside their stack:
https://github.com/gskinnerteam/flutter-defer-pointer/blob/master/example/lib/examples/simple_offset_outside_parent.dart -
a classic desktop/web style dropdown menu:
https://github.com/gskinnerteam/flutter-defer-pointer/blob/master/example/lib/examples/dropdown_menus.dart -
a animated menu based on the
flow
widget:
https://github.com/gskinnerteam/flutter-defer-pointer/blob/master/example/lib/examples/flow_menu.dart -
a auto-complete search field:
https://github.com/gskinnerteam/flutter-defer-pointer/blob/master/example/lib/examples/auto_complete.dart
manual linking
by default a deferpointer
widget will look up the closest deferredpointerhandler
using it’s current context. for more complicated use cases you can manually assign a link to bind a pointer to a handler:
final _deferredpointerlink = deferredpointerhandlerlink();
...
widget build(){
return deferredpointerhandler(
link: _deferredpointerlink,
child: padding(
padding: const edgeinsets.all(20),
child: deferpointer(
link: _deferredpointerlink,
child: ...,
)),
);
}
�� bugs/requests
if you encounter any problems please open an issue. if you feel the library is missing a feature, please raise a ticket on github and we’ll look into it. pull request are welcome.
�� license
mit license
Comments are closed.