首页 > 解决方案 > 为什么我的应用程序的状态栏会自动隐藏?

问题描述

我启动了一个颤振应用程序,并向该应用程序添加了一些代码,之后,当我在 android 设备上运行我的应用程序时,我的应用程序的状态栏会自动隐藏吗?

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.black,
        accentColor: Colors.amber,
        appBarTheme: AppBarTheme(
          textTheme: ThemeData.light().textTheme.copyWith(
                title: TextStyle(
                  color: Colors.amber,
                  fontFamily: 'OpenSans',
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
        ),
      ),
      title: 'Currency App',
      home: HomePage(),
      routes: {
        ExchangePage.routeName: (ctx) => ExchangePage(),
        GrowsPage.routeName: (ctx) => GrowsPage(),
        CurrencyPage.routeName: (ctx) => CurrencyPage(),
        ReorderListItemScreen.routeName: (ctx) => ReorderListItemScreen(),
      },
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    _portraitModeOnly();
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.amber),
        title: const Text('Currency App'),
      ),
      body: ImageCarousel(),
      drawer: DrawerCode(),
    );
  }

  void _portraitModeOnly() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }
}

这是我的代码,我搜索了各种网站,但找不到合适的,请帮助我,谢谢

标签: androidflutterstatusbar

解决方案


StatusBar 不会去任何地方,你看不到它的唯一原因是你有primaryColor黑色,这会将 AppBar 和 StatusBar 更改为相同的颜色,所以它看起来就像它消失了。如果您想独立更改 StatusBar 颜色,您可以执行以下操作:

import 'package:flutter/services.dart';  

  @override
  Widget build(BuildContext context) {

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle( statusBarColor: Colors.lightBlue ));

    return MaterialApp()

此外,“标题”属性已被弃用,appBarTheme: AppBarTheme() 使用“标题”之一 https://api.flutter.dev/flutter/material/TextTheme-class.html


推荐阅读