【问题标题】:Change status bar color when orientation changes方向更改时更改状态栏颜色
【发布时间】:2020-05-07 20:27:41
【问题描述】:

所以,我想动态更改 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 的构建函数中,每次方向改变时,在 return 语句返回 Scaffold 之前,都会调用此函数;我尝试了具有相同结果的 StatefulWidget。

日志证实了这一点。传递给函数的方向也是正确的。

但是,UI 只更新一次,第一次调用函数时,无论是纵向还是横向,UI 都会更新为每个场景的指定颜色。

当方向改变时,虽然调用了函数,但UI并没有更新。

有什么想法吗?

提前谢谢你:)

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    所以,我设法做到了,不是使用系统 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);
    }
    

    这是在 Stateful Widget 中完成的; AppThemeData 是我存储主题信息的类,Constants 是我存储在整个应用程序中使用的常量的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2015-09-26
      • 2016-11-04
      • 2017-02-02
      • 2021-11-05
      • 2018-08-27
      相关资源
      最近更新 更多