Flutter SingleChildScrollView在堆栈内部不起作用

问题描述 投票:0回答:1

因此,我试图制作一个带有滚动堆栈的屏幕,并且我使用AlignPositioned包来对齐堆栈中的已定位窗口小部件,而我尝试使用扩展窗口小部件LayoutBuilders来解决这个问题,我只是想不出如何使其动态化(https://pub.dev/packages/align_positionedthe UI

下面有很多城市名称,根据容器的高度,屏幕只能滚动到其中的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个回答
0
投票
ListView.builder小部件替换Column并删除所有AlignedPositioned,而应使用Positioned(如果可能)。删除父容器堆栈,并添加一个具有高度值的容器。它会帮助堆栈计算其大小。您必须从商品列表中获取最终尺寸。这就是我可以让您的代码按需运行的方式。

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, ) ], ), )), ); }

© www.soinside.com 2019 - 2024. All rights reserved.