简体   繁体   English

带有水平列表和子网格的Flutter可滚动主体

[英]Flutter scrollable body with horizontal list and grid as children

I'm having a hard time understanding how to best create a scrollable container for the body that holds inside children that by default are scrollable as well. 我很难理解如何最好地为身体创建一个可滚动的容器,该容器容纳默认情况下也是可滚动的子对象。

In this case the grid shouldn't scroll but it's the entire page that should scroll so you are able to see more of the elements inside the grid. 在这种情况下,网格不应该滚动,但是应该滚动整个页面,因此您可以看到网格内的更多元素。 So basically the whole content should move vertically with the addition of the ListView moving horizontally (but that works fine already) 因此,基本上,整个内容应垂直移动,而ListView则应水平移动(但这已经可以正常工作了)

I had it working but it was using a bunch of "silver" widget, and I'm hoping there's a better solution that works without using all those extra widgets. 我可以使用它,但是它使用了一堆“银色”小部件,并且我希望有一个更好的解决方案可以在不使用所有这些额外小部件的情况下工作。

Thanks 谢谢

Here's my code so far: 到目前为止,这是我的代码:

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(),
    ]);
  }
}

The result should be something like this 结果应该是这样的

在此处输入图片说明

Create List with Horizontal Scroll direction and called it as a child for Vertical Scroll direction. 使用水平滚动方向创建列表,并将其作为垂直滚动方向的子级进行调用。

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,),
);
}
}

As we want Popular Genres & Moods section also to scroll, we should not using nestedScroll . 由于我们也希望“ Popular Genres & Moods部分也要滚动,因此我们不应该使用nestedScroll In above example GridView is nested inside `ListView. 在上面的示例中, GridView嵌套在`ListView中。 Because of which when we scroll, only the GridView will scroll. 因此,当我们滚动时,只有GridView会滚动。

I used Only one ListView to achieve the similar screen. 使用一个 ListView来实现类似的屏幕。

  • Number of children = (AllGenresAndMoodsCount/2) + 1 子代数=(AllGenresAndMoodsCount / 2)+ 1

    • divide by 2 as we are having 2 elements per row 除以2,因为我们每行有2个元素
    • +1 for the first element which is horizontal scroll view. +1是水平滚动视图的第一个元素。

Please refer the code: 请参考代码:

import 'package:flutter/material.dart';

void main() {
  runApp(new Home());
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var image = new Image.network("http://www.gstatic.com/webp/gallery/1.jpg");
    var container = new Container(
      child: image,
      padding: EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0, bottom: 5.0),
      width: 200.0,
      height: 200.0,
    );

    return MaterialApp(
      title: "Scroller",
      home: Scaffold(
        body: Center(
            child: new ListView.builder(
          itemBuilder: (context, index) {
            if (index == 0) { //first row is horizontal scroll
              var singleChildScrollView = SingleChildScrollView(
                  child: Row(
                    children: <Widget>[
                      container,
                      container,
                      container,
                    ],
                  ),
                  scrollDirection: Axis.horizontal);
              return singleChildScrollView;
            } else {
              return new Row(
                children: <Widget>[container, container],
              );
            }
          },
          itemCount: 10, // 9 rows of AllGenresAndMoods + 1 row of PopularGenresAndMoods
        )),
      ),
    );
  }
}

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

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