简体   繁体   English

Flutter 永久隐藏导航栏

[英]Flutter permanently hide navigation bar

I am facing an issue in Flutter, at least with the Android emulator, which is quite annoying.我在 Flutter 中遇到了一个问题,至少在 Android 模拟器上是这样,这很烦人。

I am using a screen in full screen mode, so I wanted to get rid of the bottom navigation bar.我在全屏模式下使用屏幕,所以我想摆脱底部导航栏。

在此处输入图片说明

For that, after researching and checking here in stackoverflow, I am using the following command:为此,在 stackoverflow 中研究和检查后,我使用以下命令:

SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);

This is placed at the top of the class when the buid method starts.buid方法启动时,它被放置在类的顶部。

The problem is that, it actually works and the bottom bar goes away BUT...as soon as I interact with the screen it pops up from the bottom, overlaying anything....问题是,它实际上有效并且底部栏消失了但是......一旦我与屏幕交互,它就会从底部弹出,覆盖任何东西......

It is especially annoying because my app has a tab widget in the bottom...so as soon as I touch the screen, the bottom bar pops up...so I cannot really touch the tabs, I touch the overlaying bottom bar.这特别烦人,因为我的应用程序底部有一个标签小部件......所以一旦我触摸屏幕,底部栏就会弹出......所以我无法真正触摸标签,我触摸覆盖的底部栏。

Anyone knows about this problem or has experience it before?任何人都知道这个问题或以前经历过吗?

set this your main class before widget build, try this在构建小部件之前将此设置为您的主类,试试这个

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyApp(),
        );
      }
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        SystemChrome.setEnabledSystemUIOverlays([]);
        return Scaffold(
          appBar: AppBar(
            title: Text('Sample App'),
          ),
          body: Center(
            child: Container(
              child: Text('Demo Screen.'),
            ),
          ),
        );
      }
    }

To achieve this functionality you will need the flutter services API.要实现此功能,您将需要 Flutter 服务 API。

Example steps to implement it:实现它的示例步骤:

  • You need to import it from package:flutter/services.dart .您需要从package:flutter/services.dart导入它。
  • Place SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]) in the initState method of the stateful widget.SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top])放在有状态小部件的 initState 方法中。
  • Place SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]) in the dispose method of the stateful widget to re-enable the bottom nav when you navigate back from that screen for example.SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom])放在有状态小部件的 dispose 方法中,以便在您从该屏幕返回时重新启用底部导航。

Example code:示例代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyApp(),
        );
      }
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
        super.initState();
      }
      @override
      void dispose() {      
        SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top,SystemUiOverlay.bottom]);
        super.dispose(); 
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Sample App'),
          ),
          body: Center(
            child: Container(
              child: Text('Demo Screen.'),
            ),
          ),
        );
      }
    }

Things to know:须知:

  • It is not a good idea to put the SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]) in the build method of the widget, because the build method executes every time your widget gets rebuild and that will hurt your app performance and can cause strange bugs like appearing and disappearing bottom nav.SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top])放在小部件的构建方法中不是一个好主意,因为每次您的小部件重建时都会执行构建方法,这会损害您的应用程序性能并可能导致奇怪的错误喜欢出现和消失底部导航。
  • On Android if you have some input field where you need to use the keyboard the bottom nav will appear again and you will need to use the other method SystemChrome.restoreSystemUIOverlays() to prevent that or set it again with SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]) more info you can find for that here https://api.flutter.dev/flutter/services/SystemChrome/setEnabledSystemUIOverlays.html在 Android 上,如果您有一些需要使用键盘的输入字段,底部导航将再次出现,您将需要使用其他方法SystemChrome.restoreSystemUIOverlays()来防止或使用SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top])更多信息,你可以在这里找到https://api.flutter.dev/flutter/services/SystemChrome/setEnabledSystemUIOverlays.html
  • SystemChrome.setEnabledSystemUIOverlays() is asynchronous SystemChrome.setEnabledSystemUIOverlays()是异步的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM