简体   繁体   English

如何使 flutter 中的 ListView 可滚动?

[英]How can I make my ListView in flutter scrollable?

I am new to flutter.我是 flutter 的新手。 I am trying to build a card app and I have a ListView for the suites of each card.我正在尝试构建一个卡片应用程序,并且每个卡片的套件都有一个 ListView。 I just realized that when I flip the screen to landscape mode, I get a overflow error.我刚刚意识到,当我将屏幕翻转到横向模式时,出现溢出错误。 I figured it would be fine because I have a ListView.我认为这会很好,因为我有一个 ListView。 But nope.但是没有。 So I flipped it back to portrait and added some HUGE text to see if that would scroll and to my suprise it did not.所以我把它翻回纵向并添加了一些巨大的文字,看看它是否会滚动,令我惊讶的是它没有。

I did some research and added physics: const AlwaysScrollableScrollPhysics(), but still not working.我做了一些研究并添加了physics: const AlwaysScrollableScrollPhysics(),但仍然无法正常工作。 All help is appreciated!感谢所有帮助!

ListView(
  scrollDirection: Axis.vertical,
  physics: const AlwaysScrollableScrollPhysics(),
  shrinkWrap: true,
  children: [
    for (var item in suites)
      Text('test', style: TextStyle(fontSize: 100.0),),
    for (var item in suites)
      FlatButton(
        onPressed: () {
          var suiteId = item.suiteId;
          var suite = item.suite;
          var suiteImage = item.suiteImage;
          var suiteColor = item.suiteColor;

          print(suiteColor);

          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SuiteScreen(
                suiteId: suiteId,
                suite: suite,
                suiteImage: suiteImage,
                suiteColor: suiteColor,
              ),
            ),
          );
        },
        child: ListTile(
          leading: Icon(Icons.deck),
          title: Text(
            item.suite,
            style: TextStyle(
              fontSize: 30.0,
            ),
          ),
        ),
      ),
  ],
);

just wrap your listview with the SingleChildScrollView widget and it'll work fine after that.只需使用 SingleChildScrollView 小部件包装您的列表视图,然后它会正常工作。 You can copy paste the below code.您可以复制粘贴以下代码。

SingleChildScrollView(
  child: ListView(
  scrollDirection: Axis.vertical,
  physics: const AlwaysScrollableScrollPhysics(),
  shrinkWrap: true,
  children: [
    for (var item in suites)
      Text('test', style: TextStyle(fontSize: 100.0),),
    for (var item in suites)
      FlatButton(
        onPressed: () {
          var suiteId = item.suiteId;
          var suite = item.suite;
          var suiteImage = item.suiteImage;
          var suiteColor = item.suiteColor;

          print(suiteColor);

          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SuiteScreen(
                suiteId: suiteId,
                suite: suite,
                suiteImage: suiteImage,
                suiteColor: suiteColor,
              ),
            ),
          );
        },
        child: ListTile(
          leading: Icon(Icons.deck),
          title: Text(
            item.suite,
            style: TextStyle(
              fontSize: 30.0,
            ),
          ),
        ),
      ),
    ],
  )
);

The answer was sooooo simple!答案太简单了! Just wrap my container which was calling the listview with a Expanded widget!只需将调用列表视图的容器包装为扩展小部件即可!

Try this code试试这个代码

You can implement scrollable row in flutter using the below code您可以使用以下代码在 flutter 中实现可滚动行

and for scrollable column just change Column with Row对于可滚动列,只需将 Column 更改为 Row

SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: Row(
   children: <Widget>[
     Text('hi'),
     Text('hi'),
     Text('hi'),
   ]
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM