Skip to content

Instantly share code, notes, and snippets.

@vasyafromrussia
Created May 9, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasyafromrussia/15163bdf39d3c09ce4228b3e46eef5d0 to your computer and use it in GitHub Desktop.
Save vasyafromrussia/15163bdf39d3c09ce4228b3e46eef5d0 to your computer and use it in GitHub Desktop.
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey sliderKey = GlobalKey();
// ...
static const platform = const MethodChannel('sample/gesture');
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _setupNavigationSafeZone());
}
// ...
void _setupNavigationSafeZone() {
final context = sliderKey.currentContext;
if (context == null) {
debugPrint("Slider is not in hierarchy yet");
return;
}
final box = context.findRenderObject() as RenderBox;
final position = box.localToGlobal(Offset.zero);
final ratio = MediaQuery.of(context).devicePixelRatio;
final verticalThreshold = 10 * ratio;
final left = position.dx * ratio;
final top = position.dy * ratio;
final right = left + box.size.width * ratio;
final bottom = top + box.size.height * ratio;
final rect = Rect.fromLTRB(left, top - verticalThreshold, right, bottom + verticalThreshold);
_setSystemGestureExclusionRects([rect]);
}
Future<void> _setSystemGestureExclusionRects(List<Rect> rects) async {
try {
final rectsAsMaps = rects
.map((r) => {"top" : r.top.floor(), "left" : r.left.floor(), "bottom" : r.bottom.floor(), "right" : r.right.floor()})
.toList();
await platform.invokeMethod('setSystemGestureExclusionRects', rectsAsMaps);
} on PlatformException catch (e) {
print("Failed to set system gesture exclusion rects: ${e.toString()}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment