@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Title Text'),
      ),
      body: SafeArea(
          child: Stack(children: [
        Column(
          children: const [
            Text('short content'),
          ],
        ),
        const Positioned(
            top: 100,
            width: 320,
            child: SizedBox(
              width: 320,
              height: 50,
              child: ColoredBox(color: Colors.red),
            ))
      ])),
    );

enter image description here

我需要红色的框几乎整个屏幕与,但它流动的文本宽度.我该怎么做呢?

推荐答案

您可以使用AlignPositioned来包装堆栈的all个子对象,以便Stack可以正确地对它们进行布局.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Title Text'),
      ),
      body: SafeArea(
        child: Stack(
          children: [
            /// The column is now positioned.
            Positioned(
              top: .0,
              left: .0,
              child: Column(
                children: const [
                  ColoredBox(color: Colors.amber, child: Text('short content'))
                ],
              ),
            ),
            const Positioned(
              top: 100.0,
              width: 320.0,
              height: 50.0,
              child: ColoredBox(color: Colors.red),
            )
          ],
        ),
      ),
    );
  }

输出:

enter image description here

Flutter相关问答推荐

如何在容器中移动图标按钮?

Flutter -修改子小部件中的AppBar

如何更改ElevatedButton的 colored颜色 以按下它?

使用ThemeProvider不在亮模式和暗模式之间切换

从底部开始固定,然后使其可拖曳Flutter

如何使页面浏览量达到活动页面所要求的高度?

Flutter中不使用Container是否可以做margin

Dart 和 flutter 错误无法在您的 PATH 中找到 git

使用 Flutter 和 Dio 将图像上传到 API

即使小部件比Flutter 中的屏幕更宽,也始终显示滚动条

自定义绘画未显示 - Flutter

如何从flutter连接PostgreSQL数据库?

Flutter开发中,如何通过'nfc_manager'插件在Android设备上避免默认的New Tag Scanned弹出窗口?

允许文本小部件在容器之间溢出

围绕父组件旋转位置组件

如何正确地将 PageView 设置为 AppBar 的标题?

Flutter blue plus 库,当try 从 esp32 驱动程序获取 PlatformException 时读取特性

访问 AsyncSnapshot 中的嵌套属性

我一直在try 创建按钮以导航到第二页,但不知何故 RaisedButton 功能无法正常工作

我收到这个错误我无法解决