我使用OrientationBuilder
小部件来监听方向的变化,因为当方向发生变化时,状态栏高度会发生变化。
然后我用FlutterStatusbarManager
包来获取状态栏的高度。FlutterStatusbarManager.getHeight
是 afuture
所以需要用 a 包裹FutureBuilder
这是完整的示例代码
import 'package:flutter/material.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
class FixedStatusbarBackground extends StatelessWidget {
final Widget child;
const FixedStatusbarBackground({Key key, this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
child,
OrientationBuilder(
builder: (context, Orientation orientation) => FutureBuilder<double>(
future: FlutterStatusbarManager.getHeight,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(
height: snapshot.data,
color: Theme.of(context)
.appBarTheme
.color
.withOpacity(.7) //Colors.white.withOpacity(.7),
);
} else {
return Container();
}
}),
),
],
);
}
}
传入的child
小部件是整个CustomScrollView
.