2

How do I determine which of the two navigation bars is active via code?

1 2


The linked duplicate that this question was closed for does not answer this question. It does not allow for the distinction between the two navbars shown above.


My use case is: I would like to use a custom keyboard created by me. Taking a cue from the system keyboard, I should handle it based on the navigation bar.

4 6

In case the navbar is gesture (i.e. a line), I would just add the down arrow button. In the case of the button navigation bar instead, I should modify the active navigation bar to rotate the arrow, however I have not yet figured out how to do it or if it is possible, but that's another question

Francesco - FL
  • 603
  • 1
  • 4
  • 25
  • I edited your question for you to clarify why this question should be reopened and voted to reopen it. The linked duplicate just isn't what you're asking which is why your question should be reopened. The duplicate just being android specific doesn't cut it in my opinion since you could just write a simple Flutter plugin to interface with the native Android code. – Christopher Moore Jan 22 '23 at 22:11
  • Not that this is as answer, but: Do you mind clarifying more as to why do you want to determine which of the two navbars are shown? This is a very atypical type of problem in my experience, as usually an app doesn't need to know these details. What do you want to achieve with this, there may be a better way. – M. Prokhorov Jan 25 '23 at 14:05

1 Answers1

1

You can calculate device height depending on Android bottom navigation style.

For calculating, device_info_plus library is needed.

https://pub.dev/packages/device_info_plus

With this library, you can check real device height.

That is displayMetrics.heightPx of AndroidDeviceInfo.

And then you can calculate the Android navigation height (androidNavHeight).

final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
final screenHeight = MediaQuery.of(context).size.height;

final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
final deviceHeight = androidInfo.displayMetrics.heightPx;

final androidNavHeight = deviceHeight / devicePixelRatio - screenHeight;

If navigation style is button, the androidNavHeight will be 48.

Else if navigation style is swipe gesture, the result will be around 14.857....

debug_ing
  • 476
  • 3
  • 13