【问题标题】:Horizontal Listview inside a Stack: Horizontal viewport was given unbounded width堆栈内的水平列表视图:水平视口被赋予无限宽度
【发布时间】:2019-08-24 01:11:23
【问题描述】:

如何在 Flutter 的 Stack 中显示水平的 Listview?

Listview 是另一个(垂直)Listview 中卡片的一部分。 如果我给它一个固定宽度,我可以正确显示它,但我不应该,我希望它适应卡片宽度。

我有以下代码:

return ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) {
            final PlatformModel platform = snapshot.data[index];

            final EdgeInsets padding = index == 0
                ? const EdgeInsets.only(
                    left: 16.0,
                    right: 8.0,
                    top: 8.0,
                    bottom: 8.0,
                  )
                : const EdgeInsets.only(
                    left: 8.0,
                    right: 8.0,
                    top: 8.0,
                    bottom: 8.0,
                  );

            bloc.fetchPlatformLogo(platform.platformLogo);
            String fetcher = "platforms='${platform.id}'";
            bloc.fetchGames(fetcher);

            return Padding(
              padding: padding,
              child: Card(
                child: Container(
                  height: 250.0,
                  child: Stack(
                    fit: StackFit.expand,
                    children: <Widget>[
                      Image.network(NETWORK_URL),
                      _listGames(fetcher),
                    ],
                  ),
                ),
              ),
            );
          },
        );

Widget _listGames(String fetcher) {
    final bloc = BlocProvider.of<GamesBloc>(context);

    return Positioned(
      bottom: 5.0,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SizedBox(
          height: 200,
          child: StreamBuilder(
            stream: bloc.games,
            builder: (context,
                AsyncSnapshot<Map<String, Future<List<GameModel>>>> snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }

              return FutureBuilder(
                future: snapshot.data[fetcher],
                builder:
                    (context, AsyncSnapshot<List<GameModel>> snapshotGames) {
                  if (!snapshot.hasData || !snapshotGames.hasData) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }

                  if (snapshotGames.data.length == 0) {
                    return Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Icon(
                            Icons.warning,
                            size: 100.0,
                          ),
                          Text("Nessun gioco presente"),
                        ],
                      ),
                    );
                  }

                  return ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: snapshotGames.data.length,
                    itemBuilder: (context, index) {
                      final GameModel game = snapshotGames.data[index];
                      final lastGame = index == snapshotGames.data.length;

                      bloc.fetchGameCover(game.cover);

                      return Padding(
                        padding:
                            lastGame ? 0.0 : const EdgeInsets.only(right: 8.0),
                        child: GameCard(game: game),
                      );
                    },
                  );
                },
              );
            },
          ),
        ),
      ),
    );
  }

但我不断收到“水平视口的宽度没有限制”。

如果我说,例如

        child: SizedBox(
          height: 200,
          width: 300,
          child: StreamBuilder(

显示得很好,但当然不好。

我能做什么?

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    你可以试试这个吗..

    ListView.builder(
        shrinkWrap: true,
    ...
    

    【讨论】:

    • 已经试过了:Listview 正在显示,但它不再滚动。似乎它需要另一个容器来限制他的宽度,但我不知道该怎么做......
    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多