【问题标题】:Adding both vertical and horizontal scrolling to ListView将垂直和水平滚动添加到 ListView
【发布时间】:2019-10-20 00:14:05
【问题描述】:

如何使Rows 在垂直ListView 内,可水平滚动。

这就是我的应用的样子:

我希望用户能够水平滚动 ListView(以查看 Row 的内容)和垂直滚动以查看新列表项。这种行为就像在 Flutter 中滚动 DataTable

这里是构建方法(基于Flutter项目模板):

@override
Widget build(BuildContext context) {
  final List<String> rows = [
    "This is a row with some very long text ... That goes on and on",
    "This class is the configuration for the state.",
    "case the title) provided by the parent (in this case the App widget) and",
    "always marked \"final\"."
  ];

  return Scaffold(
    appBar: AppBar(title: Text("ListView")),
    body: ListView(
      padding: EdgeInsets.all(10),
      children: <Widget>[
        for (final String row in rows)
          Row(
            children: <Widget>[Text(row)],
          )
      ],
    ),
  );
}

更新:将 Text 包裹在 Expanded 中不起作用,因为它会导致文本换行成多行。我希望文本保留在一行上。

【问题讨论】:

  • 添加一个列表视图作为子视图并使用 scrollDirection: Axis.horizo​​ntal 。
  • @Rubens Melo 那只会让单行滚动,但是有没有办法让整个 ListView 滚动?

标签: flutter dart


【解决方案1】:

终于 经过深入搜索终于找到了这个

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: new SizedBox(
          width: 1000.0,
          child: new ListView.builder(
            itemBuilder: (BuildContext context, int i) {
              return new Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: new List.generate(5, (int j) {
                  return new Text("$i,$j");
                }),
              );
            },
          ),
        ),
      ),
    );
  }

原文出处Vote up his answer too

【讨论】:

  • 这使得Text 行可以单独滚动,但我希望能够水平滚动整个ListView,而不仅仅是单个Row
  • @S.D.检查我的更新
  • 关闭,但随后Axis.horizontal 会阻止ListView 的垂直滚动。我添加了一个视频,展示了如何在我尝试创建的内容中垂直和水平滚动。
  • @S.D.检查我的最终答案...如果您满意,请标记为正确且有用
  • SizedBox 的使用会导致问题,因为width: 1000.0, 的固定宽度,如果列表使用 会有很多空白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 2018-09-13
  • 1970-01-01
  • 2017-06-19
相关资源
最近更新 更多