方向更改时更改状态栏颜色

时间:2020-05-07 20:27:41

标签: android flutter dart

因此,我想动态更改systemNavigationBarColor和statusBarColor。这是我的尝试:

void systemColors(Orientation orientation) {
print('orientation changed -> $orientation');
if (orientation == Orientation.portrait) {
SystemChrome.setSystemUIOverlayStyle(
  SystemUiOverlayStyle(
    systemNavigationBarColor: Colors.blue,
    statusBarColor: Colors.green,
   ),
 );
} else {
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
        systemNavigationBarColor: Colors.green,
        statusBarColor: Colors.blue),
  );
}

现在,每次状态改变时,都会在StatelessWidget的build函数内部的return语句返回Scaffold之前调用此函数;我试过一个StatefulWidget,结果相同。

日志确认它。传递给函数的方向也是正确的方向。

但是,在第一次调用该功能时,无论是纵向还是横向,UI都只会更新一次,并且会针对每种情况使用指定的颜色来更新UI。

当方向改变时,尽管调用了该函数,但UI不会更新。

有什么想法吗?

预先感谢您:)

1 个答案:

答案 0 :(得分:0)

因此,我设法做到了这一点,而不是使用系统Chrome,而是通过将带有注释区域小部件的小部件包装起来,就像这样:

Orientation orientation = MediaQuery.of(context).orientation;

// return widget
return AnnotatedRegion(
  value: orientation == Orientation.portrait
         ? widget.appThemeData.systemColorsPortrait(orientation)
         : widget.appThemeData.systemColorsLandscape(orientation),
  child: Scaffold( // code continues on..

systemColorPortrait函数如下所示:

SystemUiOverlayStyle systemColorsPortrait(Orientation orientation) {
  // log
  print('systemColorsPortrait called with orientation = $orientation');

  // logic
  return SystemUiOverlayStyle(
    systemNavigationBarColor: _constants.kDarkColor,
    statusBarColor: _constants.kTransparentColor);
}

这是在有状态小部件内完成的; AppThemeData是我存储有关主题的信息的类,而Constants是我存储在整个应用程序中使用的常量的类。