【问题标题】:What is wrong with my scrollable listview?我的可滚动列表视图有什么问题?
【发布时间】:2020-12-11 22:58:01
【问题描述】:

flutter 新手,尝试制作一个包含长列表视图的可滚动页面。我尝试了一些我在不同的 SO 页面上读过的不同的东西,所以我的代码可能看起来很臃肿。我不知道为什么即使页面显示正常,并且 ListTile 的最后一张图片也显示了,但我无法向下滚动屏幕以查看该图片的其余部分。

class About extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.amberAccent,
        appBar: AppBar(
          title: Text('About this App'),
          backgroundColor: Colors.lightGreen[400],
          centerTitle: true,
        ),
        body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Text(
                      'The following people helped made my first app possible:',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontSize: 20,
                          color: Colors.black,
                          fontWeight: FontWeight.bold)),
                  ListView(
                    shrinkWrap: true,
                    padding: EdgeInsets.all(15.0),
                    children: <Widget>[
                      ListTile(
                        leading: Text('Me'),
                        title: Text(
                            'a little about myself'),
                      ),
                      ListTile(
                        leading: Text('Coworker'),
                        title: Text(
                            'Contribution made by coworker'),
                      ),
                      ListTile(
                        leading: Text('Friend'),
                        title: Text(
                            'Message about friend'),
                      ),
                      ListTile(
                        title: SizedBox(
                            height: 500.0,
                            width: 500.0,
                            child: Image.asset('images/friend.jpeg')),
                      ),
                    ],
                  ),
                ])));
  }
}```

【问题讨论】:

    标签: flutter listview singlechildscrollview


    【解决方案1】:

    在您的列表视图中使用physics: ScrollPhysics(), 以获得更详细的答案,请参阅下面的链接

    How to implement Nested ListView in Flutter?

    【讨论】:

    • 谢谢,我从你的链接中添加了物理:ClampingScrollPhysics(),现在我的页面完美滚动了。
    【解决方案2】:

    问题是您将一个高度无限的ListView 放入高度有限的Column 小部件中,因此ListView 不知道如何呈现自己。

    ListView 具有无限的高度,因为它具有滚动功能,因此它可以滚动无限数量的项目。

    Column 的高度是有限的,是屏幕的所有可用高度,所以它被限制在这个高度。

    最简单的解决方案之一是将ListView 包装在Expanded 小部件中,以告诉ListView 仅采用所有可用高度,这就是Expanded 小部件的作用。

    【讨论】:

      【解决方案3】:

      如果以后有人遇到和我一样的问题,这就是我修复代码的方法:

      对于子ListView,使用参数:

      shrinkWrap: true,
      physics: ClampingScrollPhysics(),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        相关资源
        最近更新 更多