Here is a demo where I have taken some container, textfield and listview builder,
I have wrapped all in SingleChildScrollView to avoid pixel error while opening keypad,
Here I want to give possible height to listview ,depend on item count and spacer between listview and save button
Here is my code:
List<String> paybylist=[
'A','B','C','D','E','F','G',
'H','I','J','K','L','M','N',
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(children: [
Container(
height: 100,
color: Colors.red,
child: Center(child: Text('Container1')),
),
Container(
height: 50,
//color: Colors.green,
child: Center(
child: CustomTextField(),
),
),
Text('Select Payment Mode'),
Container(
height: 400,//height should be based on available space
color: Colors.grey,
child:ListView.builder(
itemCount: paybylist.length,
itemBuilder: (context,index){
return ListTile(
title: Text(paybylist[index]),
);
}) ,
),
//want to add available space between listview and save button
Padding(
padding: const EdgeInsets.all(8.0),
child: MaterialButton(
color: Colors.blue,
onPressed: (){},child: Center(child: Text('Save')),),
)
],),
),
);
}
}
2
Answers
You cannot add
Expanded
inside theScrollable
Widget in flutter,To fill the available gap, place the
save
button inbottomNavigationBar
So now the
ListView
doesn’t need fixed height of 400 and it can span to have the full height. In fact it will span only the height of childrenMake sure you add
shrinkWrap :true
andphysics: NeverScrollableScrollPhysics()
inside theListView.builder()
Your present structure
Change it to :
You can add
shrinkWrap:true
in yourListView.builder()
and addphysics: NeverScrollableScrollPhysics()
to it otherwise your list won’t scroll.Here’s an example
Also you can add your save button either in
Stack
orBottomNavigationBar
, that way the button will be always visible.