导航到新屏幕时状态栏颜色

编程入门 行业动态 更新时间:2024-10-25 08:21:02
本文介绍了导航到新屏幕时状态栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在第一个屏幕的 build()方法中使用以下方法来更改状态栏颜色,并且工作正常。

I am using following in my build() method of 1st screen to change status bar color and it works fine.

// 1st screen's build() method SystemChrome.setSystemUIOverlayStyle( SystemUiOverlayStyle(statusBarColor: Colors.green), );

但是,当我导航到第二个屏幕时,第一个屏幕状态栏的颜色也会出现在第二个屏幕上。在第二个屏幕的 build()方法中,我正在使用具有不同颜色的相同代码

However when I navigate to 2nd screen the 1st screen status bar color appears on the 2nd screen too. And in 2nd screen's build() method, I am using the same code with different color

// 2nd screen's build() method SystemChrome.setSystemUIOverlayStyle( SystemUiOverlayStyle(statusBarColor: Colors.red), );

推荐答案

在进行 Navigator.push 到第二个屏幕。像这样

Try setting the 2nd status bar color before doing the Navigator.push to the second screen. Like this

SystemChrome.setSystemUIOverlayStyle( SystemUiOverlayStyle(statusBarColor: Colors.red), ); await Navigator.of(context).push(MaterialPageRoute( builder: (context) => SecondScreen(); ));

来自 Flutter Docs

在其生命周期与以下内容相匹配的代码中调用此API:所需的系统UI样式。例如,要更改新页面上的系统UI样式,请在推动/弹出新的PageRoute时考虑调用。

Call this API in code whose lifecycle matches that of the desired system UI styles. For instance, to change the system UI style on a new page, consider calling when pushing/popping a new PageRoute.

更多推荐

导航到新屏幕时状态栏颜色

本文发布于:2023-11-27 12:03:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:到新   状态栏   屏幕   颜色

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!