【问题标题】:Make flutter application fullscreen让 Flutter 应用全屏显示
【发布时间】:2018-03-20 06:56:39
【问题描述】:

如何使 Flutter 应用程序全屏显示。我已经知道如何隐藏状态栏Hide Android Status Bar On Flutter App。但我似乎找不到如何隐藏 android 导航栏。

【问题讨论】:

  • @FarshidABZ 您提供的链接适用于 Android SDK - Bobola 显然在寻找 Flutter 解决方案。
  • 最新的解决方案是使用setEnabledSystemUiMode

标签: android dart flutter


【解决方案1】:

和安卓状态栏一模一样。

执行SystemChrome.setEnabledSystemUIOverlays([]) 会隐藏状态栏和导航栏。

p/s : 使用这个 SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values); 禁用全屏模式。

【讨论】:

  • 我遇到了问题。我只希望在一个屏幕上出现这种行为。在 initState SystemChrome.setEnabledSystemUIOverlays([]) 中隐藏状态栏和导航栏,但在 onDispose SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values) 中不会将它们带回来。
  • 只是为了澄清:SystemChrome.setEnabledSystemUIOverlays([]); 必须添加到类的 build 方法中,但在“return”之前。
  • 这不起作用!当一个文本域获得焦点时,虚拟键盘和手机状态栏一起出现,用户在文本域中输入内容失去焦点后,状态栏仍然存在,这意味着应用程序现在没有完整屏幕。文档中说,在文本字段失去焦点后,我们只能在 1秒后再次调用 SystemChrome.setEnabledSystemUIOverlays,但是我的应用程序中有数十个文本字段,我应该如何让我的应用程序始终处于全屏模式?
  • 不认识的人,要导入'Service.dart'import 'package:flutter/services.dart';
  • 现在已弃用!有人可以告诉我替代方案吗?
【解决方案2】:
// to hide only bottom bar:
SystemChrome.setEnabledSystemUIOverlays ([SystemUiOverlay.top]);

// to hide only status bar: 
SystemChrome.setEnabledSystemUIOverlays ([SystemUiOverlay.bottom]);

// to hide both:
SystemChrome.setEnabledSystemUIOverlays ([]);

【讨论】:

  • 你搞错了。
  • @JideGuru - 是的,这是正确的......代码中有一个小错误。我在提交的编辑中建议了正确的代码。希望更正能得到同行评审的批准。
  • 我可以再次启用 statusBar 和 Navigation Bar 吗?如果是,那么程序是什么?
  • 不幸的是,如果你使用: SystemChrome.setEnabledSystemUIOverlays ([SystemUiOverlay.bottom]);并向下滑动顶部栏 - 它会显示并永久保持...
【解决方案3】:

最后我找到了一种方法来设置一个全屏类。并禁用其他类。

要设置全屏,请将此代码放入 initState()

SystemChrome.setEnabledSystemUIOverlays([]);

然后恢复正常。在 dispose()

SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);

在同一个班级。

@override
  void dispose() {
        SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    super.dispose();
  }
    
  @override
  initState() {
      SystemChrome.setEnabledSystemUIOverlays([]);
    super.initState();
  }

你不能设置

SystemChrome.restoreSystemUIOverlays();

dispose() 中。因为它已经说了

**Restores the system overlays to the last settings provided via [setEnabledSystemUIOverlays].**
May be used when the platform force enables/disables UI elements.
          
For example, when the Android keyboard disables hidden status and navigation bars,
this can be called to re-disable the bars when the keyboard is closed.
          
On Android, the system UI cannot be changed until 1 second after the previous
change. This is to prevent malware from permanently hiding navigation buttons.`

【讨论】:

    【解决方案4】:

    下面的代码非常适合全屏

    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    
    import 'package:flutter/services.dart'; 
    ////
    
    @override
    Widget build(BuildContext context) {
    ///Set color status bar
     SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
       return Scaffold(
       backgroundColor: Colors.white,
       body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.asset(
              'images/ic_splash_logo.png',
              width: 97.0,
              height: 115.0,
              fit: BoxFit.contain,
            ),
            SizedBox(
              height: 10.0,
            ),
            Text(
              'iComplain',
              style: TextStyle(fontSize: 24,
                  color: Color(0xFF174D73),
                fontFamily: 'Helvetica'
              ),
            ),
          ],
         ),
        ),
      );
    }
    

    【讨论】:

      【解决方案5】:

      使用setEnabledSystemUIMode(推荐):

      • 全屏(带恢复):

        SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
        
      • 全屏(不恢复):

        SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
        
      • 显示特定的叠加层:

        SystemChrome.setEnabledSystemUIMode(
          SystemUiMode.manual,
          overlays: [
            SystemUiOverlay.top, // Shows Status bar and hides Navigation bar
          ],
        );
        
      • 退出全屏:

        SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
        

      恢复:一旦状态和导航栏出现,它们将再次被隐藏。

      【讨论】:

        【解决方案6】:

        SystemChrome.setEnabledSystemUIOverlays([]);

        这会隐藏顶部和底部栏,但应用大小保持不变。它不会扩展到全屏高度。

        【讨论】:

        • 嗨,当顶栏被隐藏时,有人找到使用全高的方法吗?
        【解决方案7】:

        这是我的经验,如果我在initdispose 中添加SystemChrome.setEnabledSystemUIOverlays([]);,当您使用键盘时,状态栏和导航栏将返回,但如果您这样使用它,它会完美工作:

          @override
          Widget build(BuildContext context) {
        
            // To make this screen full screen.
            // It will hide status bar and notch.
            SystemChrome.setEnabledSystemUIOverlays([]);
        
            // full screen image for splash screen.
            return Container(
                    child: new Image.asset('assets/splash.png', fit: BoxFit.fill));
              }
            }
        

        在您的小部件内部

        【讨论】:

          【解决方案8】:

          使用 SystemChrome.setEnabledSystemUIOverlays([]);在你的 initState() 中像这样:

            void initState() {
              super.initState();
              print("Splash");
          
              // Start full screen
              SystemChrome.setEnabledSystemUIOverlays([]);
            }
          

          以全屏模式启动屏幕。

          接下来使用 SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);在你的 dispose() 函数中退出下一个屏幕的全屏模式。

            void dispose() {
              super.dispose();
              // Exit full screen
              SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
            }
          

          【讨论】:

            【解决方案9】:

            在 main 中使用 SystemChrome.setEnabledSystemUIOverlays([]);,如果出现错误,可以提前使用 WidgetsFlutterBinding.ensureInitialized();

            但是,这不会在应用程序中持续存在,并且在文本字段失去焦点后,您应该在 1 秒后调用 SystemChrome.restoreSystemUIOverlays()。显然"This is to prevent malware from permanently hiding navigation buttons"。 (为什么不保持向下滑动手势以防止被“卡在”应用程序中?) 在任何情况下 我的应用充满了表单,而这种方法似乎需要做很多工作。

            我想这个解决方案有点老套,但我刚刚创建了一个在后台运行的服务,每秒钟运行一个计时器,运行 SystemChrome.restoreSystemUIOverlays() 它看起来像这样:

                class FullscreenService{
              
                Timer? timer;
                static FullscreenService? _instance;
                FullscreenService._internal() {
                timer = Timer.periodic(
                Duration(seconds: 1), (Timer t) => 
                SystemChrome.restoreSystemUIOverlays);
                }
            
                factory FullscreenService() => _instance ?? FullscreenService._internal();
            
                void cancelTimer(){
                timer!.cancel();
               }
              }
            

            我希望我以后不必使用它,如果有人知道如何在保持全屏的同时避免这种情况,请告诉我......但至少它现在可以工作。

            【讨论】:

            • 如果您希望您的应用始终处于沉浸式模式,您可以使用SystemUiMode.immersiveStick。这将在一定延迟后自动重置任何覆盖。但是,当您只想隐藏状态栏时,这不适用于手动模式,例如
            • 顺便说一句,它必须是:Duration(seconds: 1), (Timer t) =&gt; SystemChrome.restoreSystemUIOverlays())}。括号不见了
            • 甚至更好:您可以使用SystemChrome.setSystemUIChangeCallback() 来监听系统ui模式的变化。那么你就不需要一个永久运行的计时器
            【解决方案10】:

            在新版本的 Flutter 中使用以下代码:

            SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
            

             SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
            

            【讨论】:

              【解决方案11】:

              initState()build() 方法中使用SystemChrome.setEnabledSystemUIOverlays([]); 调用WidgetsFlutterBinding.ensureInitialized(); 正在重建小部件并调用build() 方法两次,为了解决这个问题,我在我的main() 函数中使用了这个函数

              void main() {
              
              SystemChrome.setEnabledSystemUIOverlays([]);
              ...
              ...
              ..
              .
              

              【讨论】:

                【解决方案12】:

                AppBar 是您的导航栏,因此只需将其从您的代码中删除,如下图所示

                //新建这段代码应该可以了,AppBar是导航栏

                import 'package:flutter/material.dart';
                
                void main() => runApp(new MyApp());
                
                class MyApp extends StatelessWidget {      
                   @override
                   Widget build(BuildContext context) {
                      return new MaterialApp(
                         home: new Scaffold(
                           body: new Center(
                              child: new Text('Hello World'),
                           ),
                         ),
                      );
                   }
                }
                

                【讨论】:

                • 请添加一些说明为什么您的代码可以工作,并确保下次正确格式化您的代码。否则它是不可读的。
                • 请定义你的答案
                猜你喜欢
                • 2021-05-07
                • 1970-01-01
                • 1970-01-01
                • 2010-10-05
                • 1970-01-01
                • 2019-11-06
                • 1970-01-01
                • 2013-08-21
                • 1970-01-01
                相关资源
                最近更新 更多