Home > Mobile >  How to use listview with other widgets?
How to use listview with other widgets?

Time:10-15

I want to use ListView with other widgets , but I can't. When I use container for Listview, I can't view any other widgets. How can I do it?

 Scaffold(   
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[

              ListView.builder(),

              RaisedButton(
                child: Text('Text'),
                onPressed:(){})
])));

CodePudding user response:

Add ShrinkWrap to The ListView

 Scaffold(   
    body: SingleChildScrollView(
      child: Column(
        children: <Widget>[

           ListView(
        shrinkWrap: true,
        children:[
          Container(),
          Container(),
          ]
        ),

          RaisedButton(
            child: Text('Text'),
            onPressed:(){})
])));

for More Advanced Scrolling Challenges like More than One ListView in Column I Suggest you add a ScrollPhysics

CodePudding user response:

u need use Expanded here and set data to ListView.builder

final items = List<String>.generate(10000, (i) => 'Item $i');

Column(children: [
    Expanded(
      child: ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(items[index]),
                );
              },
            ),
    AnyWidget(...)
])

CodePudding user response:

You have to wrap your ListView widget with Expanded or if you want to wrap it with Container then you must have to give Container height or width

CodePudding user response:

This is because you are using ListView inside Column, both ListView and Column take the full screen available to them, as this way we can only see ListView on the screen, to resolve this we have to shrink ListView to its exact size, for it shrinkwrap: true is used.

ListView.Builder(
  shrinkWrap: true,
  physics: NeverScrollableScrollPhysics(),

)

physics: NeverScrollableScrollPhysics(), is used here to stop ListView scrolling, you have added SingleChildScrollView() which scroll entire page

CodePudding user response:

Try this Code...

 Scaffold(
                      body: SingleChildScrollView(
                          child: Column(children: <Widget>[
                    ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: 4,
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          return Text('Hello');
                        }),
                    RaisedButton(child: Text('Text'), onPressed: () {})
                  ])));
  • Related