Created
May 9, 2020 17:12
-
-
Save vasyafromrussia/15163bdf39d3c09ce4228b3e46eef5d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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