[英]How to change the status bar icon brightness programmatically when the sliver app bar collapsed?
I am using a simple flutter widget sliver app bar.我正在使用一个简单的 flutter 小部件银条应用栏。
and I want to switch the status bar icon brightness when the sliver app bar is collapsed.我想在折叠条应用栏折叠时切换状态栏图标亮度。 here I am listening to the scrollbar and calling function when the app bar is collapsed.
在这里,我正在收听滚动条并在应用栏折叠时调用 function。 and setting state with the appropriate brightness.
并将 state 设置为适当的亮度。
code:-代码:-
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with RouteAware {
ScrollController _scrollController;
SystemUiOverlayStyle _brightness = SystemUiOverlayStyle.light;
@override
void initState() {
this._scrollController = ScrollController()
..addListener(() => _isAppBarExpanded
? setState(
() {
_brightness = SystemUiOverlayStyle.light;
print('setState is called 1 ');
},
)
: setState(() {
print('setState is called 2 ');
_brightness = SystemUiOverlayStyle.dark;
}));
}
bool get _isAppBarExpanded {
return _scrollController.hasClients &&
_scrollController.offset > (200 - kToolbarHeight);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: _brightness,
child: Stack(
children: <Widget>[
CustomScrollView(
controller: this._scrollController,
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.grey[50],
expandedHeight: 200,
elevation: 0,
flexibleSpace: Container()
),
ExerciseList(
data: widget.data,
),
],
),
Container(),
],
),
),
);
}
}
nothing is working, what is the problem here?没有任何工作,这里有什么问题?
Your SliverAppBar
uses AppBar
internally.您的
SliverAppBar
内部使用AppBar
。 Have a look at this code, which is from AppBar:看看这段代码,它来自 AppBar:
final Brightness brightness = widget.brightness
?? appBarTheme.brightness
?? theme.primaryColorBrightness;
final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
? SystemUiOverlayStyle.light
: SystemUiOverlayStyle.dark;
So basically your SliverAppBar overrides the SystemUIOverlayStyle, so I suggest to add brightness: _isAppBarExpanded? Brightness.light: Brightness.dark,
所以基本上你的 SliverAppBar 会覆盖 SystemUIOverlayStyle,所以我建议添加
brightness: _isAppBarExpanded? Brightness.light: Brightness.dark,
brightness: _isAppBarExpanded? Brightness.light: Brightness.dark,
to your SliverAppBar . brightness: _isAppBarExpanded? Brightness.light: Brightness.dark,
到您的 SliverAppBar 。
Plase checkout my code and if it solve your problem then let me know:)请检查我的代码,如果它解决了您的问题,请告诉我:)
I fixed your code as below:我将您的代码固定如下:
import 'package:flutter/material.dart';
main() => runApp(MaterialApp(
home: MyWidget(),
));
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with RouteAware {
ScrollController _scrollController;
bool _isAppBarCollapsed = false;
@override
void initState() {
super.initState();
this._scrollController = ScrollController()
..addListener(() {
if (isCollapsed() && !_isAppBarCollapsed) {
setState(() {
_isAppBarCollapsed = true;
});
} else if (!isCollapsed() && _isAppBarCollapsed) {
setState(() {
_isAppBarCollapsed = false;
});
}
});
}
bool isCollapsed() {
return _scrollController.hasClients &&
_scrollController.offset > (280 - kToolbarHeight);
}
@override
Widget build(BuildContext context) {
return SafeArea(
top: _isAppBarCollapsed,
bottom: false,
right: false,
left: false,
child: Scaffold(
body: Stack(
children: <Widget>[
CustomScrollView(
controller: this._scrollController,
slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.green /*Colors.grey[50]*/,
expandedHeight: 200,
elevation: 0,
flexibleSpace: Container()),
SliverList(
delegate: SliverChildListDelegate([
Container(
height: 2000,
child: Text('item 9'),
color: Colors.blue,
),
]))
],
),
Container(),
],
),
),
);
}
}
I believe there are some issues with Appbar
and SliverApppbar
brightness
property, specially in ios.我相信
Appbar
和SliverApppbar
brightness
属性存在一些问题,特别是在 ios 中。 ( some quick googling brought this https://github.com/flutter/flutter/issues/41067 ) (一些快速谷歌搜索带来了这个https://github.com/flutter/flutter/issues/41067 )
What i have successfully done in the past, was to wrap my Scaffold
in a Theme
and override the appBarTheme
like below:我过去成功完成的是将我的
Scaffold
包装在一个Theme
中并覆盖appBarTheme
,如下所示:
Theme(
data: Theme.of(context).copyWith(
appBarTheme: AppBarTheme(
brightness: _isAppBarCollapsed ? Brightness.light : Brightness.dark,
),
),
child: Scaffold(...),
);
I hope this solves your issue.我希望这能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.