我有一个小部件演示了Flutter 应用程序的渲染.正如下面所写的,文本在navigationBar的正下方可见.然而,如果你把backgroundColor注释掉,它就看不见了.为什么?

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      home: CupertinoPageScaffold(
        navigationBar: const CupertinoNavigationBar(
          middle: Text("Settings"),
          previousPageTitle: "Back",
          backgroundColor: Colors.blue,
        ),
        child: Column(
          children: const <Widget>[
            Text("Hello World!"),
          ],
        ),
      ),
    );
  }
}

Without blue color: Image without blue color

With blue color: Image with blue color

推荐答案

高度不会随backgroundColor而改变,CupertinoPageScaffold's documentation是这样说的:

当内容为半透明时,可以在导航栏下滑动.在里面

这就是为什么你的文本是隐藏的,当它的 colored颜色 是半透明的时候,它只是在条下面.通过使用Colors.blue,您将获得不透明的 colored颜色 .

你可以try 使用backgroundColor: Colors.transparent,结果将与没有 colored颜色 相同.

要修复此行为,可以使用SafeArea小部件包装Column:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      home: CupertinoPageScaffold(
        navigationBar: const CupertinoNavigationBar(
          middle: Text("Settings"),
          previousPageTitle: "Back",
        ),
        child: SafeArea(
          child: Column(
            children: const <Widget>[
              Text("Hello World!"),
            ],
          ),
        ),
      ),
    );
  }
}

Try the full example on DartPad

Flutter相关问答推荐

使用自定义控制器将项插入到ListView中时行为不正确

将目标平台设置为Flutter 构建

如何在WidgetRef引用外部的函数中使用Riverpod刷新数据

如何处理Flutter Riverpod异步通知器中的状态和数据库

如何在容器小部件中的图像旁边添加文本?

Flutter 隔离.使用多个参数运行

查询满足AND条件的文档的FiRestore

'Flutter的无效常数值错误

如何在Flutter Webview包中触发基于URL导航的事件

不要跨同步间隔使用BuildContext

使用 forLoops 在 Dart 中进行单元测试会返回 stackoverflow

如何在 Flutter 中设计像 zomato 应用程序一样的搜索字段?

Flutter 使用扩展的 TextField 定位小部件

Flutter:使用 GlobalKey 访问 BlocProvider

当键盘隐藏表单时该怎么办?

如何使 flutter 2 手指zoom ?

flutter 创建一个带有 2 个图标的按钮

Flutter Web Responsiveness 效率不高

为什么这个参数类型不能分配给参数类型?

阴影效果只出现在一张卡片的左边 在Flutter 中?