首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当定义SystemUiMode以隐藏状态栏时,如何获得状态栏高度?

当定义SystemUiMode以隐藏状态栏时,如何获得状态栏高度?
EN

Stack Overflow用户
提问于 2022-04-19 05:31:10
回答 1查看 262关注 0票数 0

我正在覆盖我的SystemUiMode,代码如下:

代码语言:javascript
运行
复制
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

我需要这个SystemUiMode。好吧,我在SingleChildScrollView中有小部件(比如说一个表单)。当键盘出现时,我在ScrollView中的内容足够大,足以填满所有可用的空间,它会击中屏幕的顶部。我想要一个设计,我的SingleChildScroview有一个相同大小的状态栏顶部填充。

我试过:

  1. To use SafeArea:但是它不起作用,在第一分钟,我的小部件填充了整个可用的空间,忽略了状态栏的高度,然后它在预期的布局之间闪烁,然后再进行填充。下面是代码:

代码语言:javascript
运行
复制
class Test extends StatelessWidget {
  const Test({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.green,
        body: Center(
          child: SingleChildScrollView(
            child: Center(
              child: Container(
                width: size.width * .8,
                height: size.height * .9,
                color: Colors.red,
                child: Center(child: TextField()),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

  1. I试着听MediaQuery的变化并存储高度的值,但是当键盘第一次出现(有时也会在一秒内)时,它就填满了整个可用的空间。

代码语言:javascript
运行
复制
  static double topPadding = 0;

  setTopPadding(double newPad) {
    if (newPad > topPadding) topPadding = newPad;
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

    setTopPadding(MediaQuery.of(context).viewPadding.top);

    return Scaffold(
      backgroundColor: Colors.green,
      body: Center(
        child: Padding(
          padding: EdgeInsets.only(top: topPadding),
          child: SingleChildScrollView(

            child: Center(
              child: Container(
                  width: size.width * .8,
                  height: size.height * .9,
                  color: Colors.red,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Text("A"),
                      TextField(),
                      Text("B"),
                      TextField(),
                    ],
                  )),
            ),
          ),
        ),
      ),
    );
  }

如何获得状态栏的静态高度?

EN

回答 1

Stack Overflow用户

发布于 2022-04-19 06:15:03

您可以将Colors.transparent交给statusBarColor of systemOverlayStyle

使statusBar消失

最好使用CustomScrollView Widget而不是SingleChildScrollView..。

代码语言:javascript
运行
复制
    CustomScrollView(
                physics: const BouncingScrollPhysics(), slivers: [
              SliverAppBar(
                systemOverlayStyle:
                const SystemUiOverlayStyle(statusBarColor: Colors.transparent ),
                flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                title: 'hello',
                background: NetworkImage(imageUrl: networkImage),
),
),
                SliverList(
                delegate: SliverChildListDelegate(
                  [
                    Container(
                      decoration: const BoxDecoration(
                          borderRadius: BorderRadius.only(
                              topRight: Radius.circular(30),
                              topLeft: Radius.circular(30))),
                      padding: const EdgeInsets.symmetric(horizontal: 14.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: getScreenHeight(10)),
                          Text(
                            name,
                            maxLines: 1,
                            style: Theme.of(context).textTheme.subtitle1,
                            textAlign: TextAlign.start,
                          ),]
),)
]);

          ),
 ),
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71920093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档