【问题标题】:Flutter SingleChildScrollView not working inside StackFlutter SingleChildScrollView 在 Stack 中不起作用
【发布时间】:2020-03-09 19:04:54
【问题描述】:

所以我正在尝试制作一个具有滚动堆栈的屏幕,并且我正在使用 AlignPositioned 包来对齐堆栈中的定位小部件我尝试使用扩展小部件 LayoutBuilders 我只是不知道如何制作它动态 (https://pub.dev/packages/align_positioned)

下面有很多城市名称,根据容器的高度,屏幕只能滚动到其中的1-2个。这是我当前的代码,每当我为 Container 设置恒定高度时,UI 不会引发错误,但屏幕不能完全滚动

return SafeArea(
  child: Scaffold(
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          child: Stack(
            children: <Widget>[
              Positioned(
                top: 0,
                child: TopArc(), // The blue background circle
              ),
              AlignPositioned(
                dy: 40,
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 16, vertical: 4),
                  child: AutoSizeText(
                    "Choose Your Location",
                    stepGranularity: 1,
                    maxLines: 1,
                    style: TextStyle(
                      fontSize: 38,
                      color: Colors.white,
                      // This is the maximum value then the AutoSizeText widget will shrink it until it can fit a single line.
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              AlignPositioned(
                dy: 90,
                alignment: Alignment.topCenter,
                child: StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance
                      .collection('locations')
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.hasError) {
                      return CircularProgressIndicator();
                    }
                    if (!snapshot.hasData) {
                      return CircularProgressIndicator();
                    }
                    return ContentTile(
                      child: ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (ctx, int i) {
                          final document = snapshot.data.documents[i];

                          return LocationExpansionTile(
                              document.documentID, document["cityName"]);
                        },
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      )),
);

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    我正在处理几乎相同的问题,如果您的 SingleChildScrollView 在定位小部件内,您将必须精确定位的顶部和底部,在我的情况下,我没有按下底部,所以我没有可以向下滚动

    【讨论】:

      【解决方案2】:

      ListView.builder 替换为 Column 小部件并删除所有 AlignedPositioned 而使用 Positioned(如果可能的话)。移除 Stack 的父 Container 并添加一个具有高度值的 Container。它将帮助堆栈计算它的大小。您必须从项目列表中获取最终尺寸。这就是我可以让您的代码随心所欲运行的方式。

      var _itemsView = GlobalKey();
      
      double _stackHeight;
      
      @override
      void initState() {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          RenderBox stackRB =
              _itemsView.currentContext.findRenderObject() as RenderBox;
          setState(() {
            _stackHeight = stackRB.size.height;
          });
        });
        super.initState();
      }
      
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
              body: SingleChildScrollView(
            child: Stack(
              children: <Widget>[
                Positioned(
                  ...
                ),
                Positioned(
                  ...
                ),
                Positioned(
                  key: _itemsView,
                  top: 90,
                  child: Column(
                    children: _data.map((i) {
                        final document = snapshot.data.documents[i];
                        return LocationExpansionTile(
                            item.documentID, item["cityName"]);
                      }),
                  ),
                ),
                Container(
                  height: _stackHeight + 90,
                )
              ],
            ),
          )),
        );
      }
      

      【讨论】:

      • 我无法构建它抛出的屏幕“方法 'findRenderObject' 在 null 上被调用。”这个错误
      • @ahmetakil 您是否将 _itemsView 设置为正确的小部件?
      • 从对齐小部件更改为定位。它对我有用。谢谢
      • 我已经尝试了很多在小型设备上滚动的方法,但你是目前唯一的解决方案。谢谢你的例子。
      猜你喜欢
      • 2021-08-29
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      相关资源
      最近更新 更多