dart - 具有水平列表和网格的 Flutter 可滚动主体作为子项

我很难理解如何最好地为 body 创建一个可滚动的容器,该容器包含在默认情况下也是可滚动的子元素中。

在这种情况下,网格不应该滚动,而是应该滚动整个页面,这样您就可以看到网格内的更多元素。所以基本上整个内容应该随着 ListView 水平移动而垂直移动(但这已经很好了)

我让它工作了,但它使用了一堆“银”小部件,我希望有一个更好的解决方案可以在不使用所有这些额外小部件的情况下工作。

谢谢

到目前为止,这是我的代码:

class GenresAndMoodsPage extends AbstractPage {
  @override
  String getTitle() => 'Genres & Moods';

  @override
  int getPageBottomBarIndex() => BottomBarItems.Browse.index;

  static const kListHeight = 150.0;

  Widget _buildHorizontalList() => SizedBox(
        height: kListHeight,
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: 20,
          itemBuilder: (_, index) =>
              CTile(heading: 'Hip Hop', subheading: '623 Beats'),
        ),
      );

  Widget _buildGrid() => GridView.count(
        crossAxisCount: 2,
        crossAxisSpacing: LayoutSpacing.sm,
        mainAxisSpacing: LayoutSpacing.sm,
        children: List.generate(10, (index) {
          return CTile(
            padding: false,
            heading: 'Kevin Gates Type Beat',
            subheading: '623 FOLLOWERS',
            width: double.infinity,
          );
        }),
      );

  @override
  Widget buildBody(_) {
    return ListView(children: [
      CSectionHeading('Popular Genres & Moods'),
      _buildHorizontalList(),
      CSectionHeading('All Genres & Moods'),
      _buildGrid(),
    ]);
  }
}

结果应该是这样的

最佳答案

创建具有水平滚动方向的列表并将其称为垂直滚动方向的子项。

body: new ListView.builder(itemBuilder: (context, index){
            return new HorizList();
          })

class HorizList extends StatelessWidget{
 @override
 Widget build(BuildContext context) {
 return new Container(
  height: 100.0,

  child: new ListView.builder(itemBuilder: (context, index){
    return new Card(child: new Container(width: 80.0,
    child: new Text('Hello'),alignment: Alignment.center,));
  }, scrollDirection: Axis.horizontal,),
);
}
}

https://stackoverflow.com/questions/52078972/

相关文章:

dart - 在 Flutter 中从文件中读取二维码

dart - Flutter 小部件测试 : StreamBuilder snapshot has

dart - flutter 错误 : Could not find the correct Sco

flutter - 加载数据时制作骨架屏幕的最简单方法

dart - 我可以在未来的构建器上使用多种方法吗?

dart - 如何在没有 Scaffold.drawer 的情况下使用 Drawer?

flutter - Sizedbox 与填充列和行中的距离

firebase - Flutter - firebase_auth updateProfile 方

dart - 在初始化程序中只能访问静态成员。 Dart2.0

flutter - 内置值对象的 setter 是什么