Flutter:将iOS状态栏文本颜色更改为黑色

问题描述 投票:-2回答:3

我想用package:flutter/services.dart包更改状态栏的颜色,但是不起作用。我正在使用Mac和iOS模拟器:

  • 莫哈韦沙漠10.14.6
  • iOS 12.2模拟器/ Xr
  • Flutter 1.9.1 + hotfix.2
  • 工具•Dart 2.5.0
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

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

    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(
        statusBarColor: Colors.red // <-- doesn't work
      )
    );

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
... // other stuff

enter image description here

即使将其放在main函数中:

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

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarColor: Colors.red,
    )
  );
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
    .then((_) => runApp(MyApp()));
}
... // the rest code here

因此,如果我想将appBar背景颜色更改为白色,则会得到这个。

enter image description here

尚未针对Android进行测试。此问题仅与iOS模拟器有关吗?如何解决?

U.P.D。

这个问题开始使我发疯。

flutter dart flutter-layout
3个回答
0
投票

尝试一下

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('Title'),
          backgroundColor: Colors.red,
          flexibleSpace: SafeArea(
            child: Container(
              color: Colors.blue,
            ),
          ),
        ),
        body: Container(),
      ),
    );
  }
}

0
投票

编辑:

appBar: AppBar(
  elevation: 0,
  brightness: Brightness.light, // this makes status bar text color black
  backgroundColor: Colors.white,
)

输出:

enter image description here


[statusBarColor只能在Android中进行更改,而不能在iOS中进行更改,并且如果您尝试通过某些变通办法进行更改,则Apple可能会拒绝您的应用程序,因为它们不希望您使用不同的AppBar和状态栏颜色。] >

AppBar(backgroundColor: Colors.red) // this changes both AppBar and status bar color in iOS

Apple希望您坚持自己的设计,这就是为什么更改statusBarColor对iOS没有影响的原因。


-1
投票

尝试:

© www.soinside.com 2019 - 2024. All rights reserved.