Horizontal ListView inside a Vertical ScrollView in Flutter

前端 未结 2 508
情歌与酒
情歌与酒 2021-01-30 04:45

I am trying to achieve a very common behavior nowadays which is to have a horizontal List within another widget which is at the same time scrollable. Think something like the ho

2条回答
  •  -上瘾入骨i
    2021-01-30 05:07

    Well, Your Code Work Fine with wrapping your- ListView.builder with Expanded Widget & setting mainAxisSize: MainAxisSize.min, of Column Widget.

    E.x Code of what you Have.

     body: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                'Headline',
                style: TextStyle(fontSize: 18),
              ),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: 15,
                  itemBuilder: (BuildContext context, int index) => Card(
                        child: Center(child: Text('Dummy Card Text')),
                      ),
                ),
              ),
              Text(
                'Demo Headline 2',
                style: TextStyle(fontSize: 18),
              ),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  itemBuilder: (ctx,int){
                    return Card(
                      child: ListTile(
                          title: Text('Motivation $int'),
                          subtitle: Text('this is a description of the motivation')),
                    );
                  },
                ),
              ),
            ],
          ),
    

    Update:

    Whole page Is Scroll-able with - SingleChildScrollView.

    body: SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            'Headline',
            style: TextStyle(fontSize: 18),
          ),
          SizedBox(
            height: 200.0,
            child: ListView.builder(
              physics: ClampingScrollPhysics(),
              shrinkWrap: true,
              scrollDirection: Axis.horizontal,
              itemCount: 15,
              itemBuilder: (BuildContext context, int index) => Card(
                    child: Center(child: Text('Dummy Card Text')),
                  ),
            ),
          ),
          Text(
            'Demo Headline 2',
            style: TextStyle(fontSize: 18),
          ),
          Card(
            child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
          ),
          Card(
            child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
          ),
          Card(
            child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
          ),
          Card(
            child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
          ),
          Card(
            child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
          ),
        ],
      ),
    ),
    

提交回复
热议问题