带 Tiles 的 ListView 在 Flutter 中不起作用

标签 listview flutter widget tile stateless

为什么我的 ListView 没有显示预期的图 block ?

class DepartureCell extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 20,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: new Text("Test"),
          onTap: () {},
        );
      }
    );
  }
}

错误提示:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performResize():
flutter: Vertical viewport was given unbounded height.
flutter: Viewports expand in the scrolling direction to fill their container.In this case, a vertical
flutter: viewport was given an unlimited amount of vertical space in which to expand. This situation
flutter: typically happens when a scrollable widget is nested inside another scrollable widget.
flutter: If this widget is always nested in a scrollable widget there is no need to use a viewport because
flutter: there will always be enough vertical space for the children. In this case, consider using a Column
flutter: instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
flutter: the height of the viewport to the sum of the heights of its children.

由于未绑定(bind)高度出现问题,我还尝试了以下操作:

代码也不起作用:

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 20,
      itemBuilder: (BuildContext context, int index) {
        return SizedBox(
          width: double.infinity,
          height: 40.0,
          child: ListTile(
            title: Text("Test"),
            onTap: () {},
            trailing: Text("Test"),
          ),
        );
      }
    );
  }
}

我怎样才能让它在 Flutter 中工作???

我也尝试过使用Column - 但同样的事情......不起作用!

这里的列代码再次不起作用:

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ListView.builder(
          padding: EdgeInsets.all(0.0),
          itemCount: 10,
          itemBuilder: (BuildContext context, int position) {
            return SizedBox(
              width: double.infinity,
              height: 40.0,
              child:ListTile(
                title: Text("Test"),
                onTap: () {},
                trailing: Text("Test"),
              ),
            );
          }
        ),
      ]
    );
  }
}

最佳答案

我不知道你之前的代码是什么。如果您可以发布您之前的代码,我可以向您解释该异常。

这是代码的简化版本。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("CH Station Demo"),
          ),
          body: DepartureCell()),
      //home: TrackerApp(),
    );
  }
}

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        padding: EdgeInsets.all(8.0),
        itemCount: 10,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("Test"),
            onTap: () {},
            trailing: Text("Test2"),
          );
        });
  }
}

关于带 Tiles 的 ListView 在 Flutter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53895366/

相关文章:

php - 如何在 php 中动态组合图像和文本

android - 使用 AutoCompleTextView 基于 BaseAdapter 过滤带图像的 Listview

wpf - ListView 中的选定项目不会取消选择

android - 如何在我的应用程序中使用来自 Github 的代码

flutter - 将 Flutter 小部件变换/平移到其宽度的负 50%

android - Android 13 的 flutter 相机 : my app keeps crashing

android - 设置 Activity 后小部件未显示在主屏幕中

javascript - 为什么这个 itemDataSource 没有被清除?

android - 在 android 列表适配器中,如果 convertView 为 null 而不是 null 怎么办?任何人都可以解释下面的 getView 方法

android - 如何在 AndroidManifest.xml 中隐藏 API key