Flutter-如何为状态背彩设置梯度(使用SafeArea)?

发布于 2025-01-25 05:01:53 字数 1270 浏览 2 评论 0原文

是否可以明确设置状态栏的梯度颜色? “状态巴尔科勒”期望颜色,但是渐变呢?如何在梯度中绘制状态栏?如果我使用SafeArea,则状态栏为白色。但是我的sliverappbar涂有梯度颜色。

Scaffold(
        body: SafeArea(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: Text(
                    'My App',
                    style: TextStyle(
                      fontSize: 16.0,
                    ),
                  ),
                  background: Container(
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        colors: <Color>[
                          Color(0xFF50AC5B),
                          Color(0xFF92C156),
                        ],
                      ),
                    ),
                  ),
                ),
                elevation: 0.0,
                floating: true,
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                  [
                    //...
                  ],
                ),
              ),
            ],
          ),
        ),
        backgroundColor: const Color(0xFFFBFDFF),
    );
  }
}

Is it possible to explicitly set gradient color for the status bar? 'statusBarColor' expects Color, but what about gradient? How to paint status bar in gradient? If I use SafeArea, status bar is white. But my SliverAppBar is painted in gradient color.

Scaffold(
        body: SafeArea(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: Text(
                    'My App',
                    style: TextStyle(
                      fontSize: 16.0,
                    ),
                  ),
                  background: Container(
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        colors: <Color>[
                          Color(0xFF50AC5B),
                          Color(0xFF92C156),
                        ],
                      ),
                    ),
                  ),
                ),
                elevation: 0.0,
                floating: true,
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                  [
                    //...
                  ],
                ),
              ),
            ],
          ),
        ),
        backgroundColor: const Color(0xFFFBFDFF),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2025-02-01 05:01:53

以下工作和测试代码:

说明:

我们如何获得这个红色阴影状态栏?

我们已经用脚手架包裹了Safearea小部件。在身体属性中,我们使用具有梯度颜色的容器,因此它占据了整个屏幕...如果我们没有Appbar的身体,则将整个屏幕作为一个主体。然后,我们可以使用其他一些小部件(根据我们的要求)

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Color.fromRGBO(232, 66, 66, 1.0),
              Color.fromRGBO(231, 33, 33, 1.0),
              Color.fromRGBO(238, 5, 5, 1.0)
            ],
          )
        ),
        child: SafeArea(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: const Text(
                    'My App',
                    style: TextStyle(
                      fontSize: 16.0,
                    ),
                  ),
                  background: Container(
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        colors: <Color>[
                          Color(0xFF50AC5B),
                          Color(0xFF92C156),
                        ],
                      ),
                    ),
                  ),
                ),
                elevation: 0.0,
                floating: true,
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                  [
                    Container(
                      color: Colors.white,
                      height: MediaQuery.of(context).size.height,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

”在此处输入图像说明”

Below worked and tested code :

Explanation :

How we get this red shaded status bar?

We have wrapped SafeArea widget by scaffold. In body property we have using container with gradient color so it occupies whole screen...If we use body without appbar it takes whole screen as a body. then we can use remaining body spaces by some other Widgets(as per our requirement)

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Color.fromRGBO(232, 66, 66, 1.0),
              Color.fromRGBO(231, 33, 33, 1.0),
              Color.fromRGBO(238, 5, 5, 1.0)
            ],
          )
        ),
        child: SafeArea(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: const Text(
                    'My App',
                    style: TextStyle(
                      fontSize: 16.0,
                    ),
                  ),
                  background: Container(
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        colors: <Color>[
                          Color(0xFF50AC5B),
                          Color(0xFF92C156),
                        ],
                      ),
                    ),
                  ),
                ),
                elevation: 0.0,
                floating: true,
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                  [
                    Container(
                      color: Colors.white,
                      height: MediaQuery.of(context).size.height,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

enter image description here

§普罗旺斯的薰衣草 2025-02-01 05:01:53

则将Safearea包裹到一个添加背景梯度的容器中

            child: Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: <Color>[
                    Color(0xFF50AC5B),
                    Color(0xFF92C156),
                  ],
                ),
              ),
              child: SafeArea(
                child: // your code,
              ),
            ),

如果不需要,

          SafeArea(
            top: false,
            child: // your code,
          ),

,或者在应用程序的顶部关闭Safearea,例如这样:让我知道任何解决方案是否有效。

Wrap your SafeArea into a Container that adds a background gradient

            child: Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: <Color>[
                    Color(0xFF50AC5B),
                    Color(0xFF92C156),
                  ],
                ),
              ),
              child: SafeArea(
                child: // your code,
              ),
            ),

or just turn off your SafeArea in the top portion of the app if it's not required there, like this:

          SafeArea(
            top: false,
            child: // your code,
          ),

Let me know if any of this solution works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文