问题
I want to change status bar color with package:flutter/services.dart
package but it doesn't work. I am using Mac and iOS simulator:
- Mojave 10.14.6
- iOS 12.2 simulator / Xr
- Flutter 1.9.1+hotfix.2
- Tools • 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
Even if I put it in the main
function:
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
As a result I get this if I want to change appBar
background color to white.
Haven't tested it for android yet. Is this issue related only to iOS simulator or so? How to fix it?
U.P.D.
This issue starts to drive my nuts.
回答1:
Edit:
appBar: AppBar(
elevation: 0,
brightness: Brightness.light, // this makes status bar text color black
backgroundColor: Colors.white,
)
Output:
statusBarColor
can only be changed in Android and not in iOS, and probably Apple can reject your app if you try do so by some workarounds because they don't want you to have different AppBar
and status bar color.
AppBar(backgroundColor: Colors.red) // this changes both AppBar and status bar color in iOS
Apple wants you to stick to their designs which is why changing statusBarColor
has no effect on iOS.
回答2:
Try them:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
statusBarColor: Colors.white, // this one for android
statusBarBrightness: Brightness.light// this one for iOS
));
回答3:
Try this
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(0),
child: AppBar( // Here we create one to set status bar color
backgroundColor: Colors.black, // Set any color of status bar you want;
or it defaults to your theme's primary color
)
)
);
}
来源:https://stackoverflow.com/questions/58270728/flutter-change-status-bar-color-in-ios