dart - 如何使用 SingleChildScrollView 使 Stack 布局可滚动?

标签 dart flutter flutter-layout

我正在尝试使用 SingleChildScrollView 使堆栈布局可滚动,但它不滚动。这里应该使用SingleChildScrollView吗?

我想我已经给出了足够的描述来让任何人理解我的问题。此处提供更多文本以满足 StackOverflow 提出问题的要求。为此表示歉意。

这是示例代码。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          child: Center(
            child: LayoutBuilder(
              builder:
                  (BuildContext context, BoxConstraints viewportConstraints) {
                return SingleChildScrollView(
                  child: ConstrainedBox(
                    constraints: BoxConstraints(
                      minHeight: viewportConstraints.maxHeight,
                    ),
                    child: IntrinsicHeight(
                      child: Column(
                        children: <Widget>[
                          Container(
                            // A fixed-height child.
                            color: Colors.white,
                            height: 120.0,
                          ),
                          Expanded(
                            // A flexible child that will grow to fit the viewport but
                            // still be at least as big as necessary to fit its contents.
                            child: Container(
                              color: Colors.blue,
                              //height: 120.0,
                              child: Stack(
                                children: <Widget>[
                                  Positioned(
                                    top: 0,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.red[100],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 50,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.red[200],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 100,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.red[300],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 150,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.green[100],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 200,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.green[200],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 250,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.green[300],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 300,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.yellow[100],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 350,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.yellow[200],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    top: 400,
                                    left: 0,
                                    right: 0,
                                    child: Container(
                                      color: Colors.yellow[300],
                                      child: SizedBox(
                                        height: 300,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  } 

最佳答案

这取决于 StackView 的大小。例如,您可以使 Stack 的子项之一未定位。然后这个 child 将影响整个堆栈 View 的大小。

SingleChildScrollView(
  child: Stack(
    children: <Widget>[
      Container(
        height: 5000,
      ),
      Positioned(
        top: 100,
        left: 100,
        width: 1000,
        height: 1000,
        child: Container(color: Colors.red),
      )
    ],
  ),
)

关于dart - 如何使用 SingleChildScrollView 使 Stack 布局可滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359662/

相关文章:

dart - 在服务器端验证时来自 google_sign_in 插件的 token 无效

flutter - FutureBuilder 和 MaterialApp 的导航问题

dart - 如何使用 android_intent 通过 flutter 打开位置设置

flutter - RenderFlex 在底部溢出了 41 个像素。相关的导致错误的小部件是 Column

flutter - TextFormField的后缀图标问题

datetime - Flutter - 将分钟转换为 H :M

dart - 为什么2.0(数字2.0)同时是double和int?

ListView.builder 上 initialScrollOffset 的性能问题

image - Flutter:将圆形图像放在角落

flutter - Flutter是否能够在运行时动态加载和构建窗口小部件?