【问题标题】:How to remove Android back buttons on MaterialApp on Flutter?如何在 Flutter 中删除 MaterialApp 上的 Android 后退按钮?
【发布时间】:2021-03-24 15:31:45
【问题描述】:
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
  OrientationSingleton.left = true;
  SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
      .then((_) {
    runApp(new MyApp());
  });
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

这是我的应用程序。我找到了一些关于如何删除它的教程:flutter remove back button on appbar 但它适用于 AppBar。我尝试让我的应用在 AppBar 上运行,但我得到了

MediaQuery.of() called with a context that does not contain a MediaQuery.

因为我在我的应用程序中依赖MediaQuery.of()

那么,对于MaterialApp,如何在 Flutter 上移除 Android 的返回、主页和方形按钮?

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    如错误消息中所述,SystemChrome 需要 context - 调用它的地方可能是 WidgetinitState() 方法:

    class MyApp extends StatefulWidget {
      const MyApp({ Key key }) : super(key: key);
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
       void initState() {
         super.initState();
         SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
         OrientationSingleton.left = true;
         SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
       }
    
       @override
       Widget build(BuildContext context) {
          return MaterialApp(); // your MaterialApp class
       }
    }
    

    隐藏 Android 上的系统底栏,一种选择是调用setEnabledSystemUIOverlays() 函数并使用一个空列表:

    SystemChrome.setEnabledSystemUIOverlays([]);
    

    但是,并非所有 Android 设备都支持此功能。

    【讨论】:

    • 在 initState() 中做这些事情并没有奏效,而是像我一样在 main() 上做这些事情。 super.initState(); 也是不可能的。
    • 我已编辑代码以支持Stateful 小部件,这是使用initState() 功能所必需的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多