请查看代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: "test",
theme: ThemeData(primarySwatch: Colors.blueGrey),
home: Scaffold(body: MyWidget()),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); // only works initially, but not when navigating back from PageRoute
return Column(
children: <Widget>[
Container(height: 80.0, color: Colors.orange),
const SizedBox(height: 100.0),
RaisedButton(child: Text('Next'),
onPressed: () {Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen())).then((value) {
_refreshSystemStatusColor(); // doesn't work
});},
)
],
);
}
void _refreshSystemStatusColor() {
setState(() {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
});
}
}
class NewScreen extends StatefulWidget {
@override
_NewScreenState createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
@override
void dispose() {
// SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); // only this line works. I don't want to convert all my widgets to StatefulWidget
super.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); // doesn't work
return Future.value(true);
},
child: Scaffold(appBar: AppBar())
);
}
}
您可以看到我尝试了三种方法来将系统状态栏的字体颜色设置回:
到目前为止,前两种方法不起作用,仅在dispose()
上进行设置即可。但是我觉得这真的很丑。我不想仅出于此目的将所有小部件都转换为StatefulWidget。
有人有更好的解决方案吗?非常感谢。
答案 0 :(得分:1)
使用脚手架时,您可以使用 AnnotatedRegion 设置系统覆盖样式。当您将脚手架与应用栏一起使用时,可以设置属性亮度来更改系统覆盖样式。在此示例中,没有必要使用StatefulWidgets。
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
const ImageFromCDN = ({ src }) => (
<Fragment>
<h5>Image From CDN</h5>
<img src={src} alt="example.png" />
</Fragment>
);
ImageFromCDN.propTypes = {
src: PropTypes.string.isRequired,
};
export default ImageFromCDN;
希望获得帮助。