I'm not sure if I'm missing something. I'm playing around with Flutter and I want to build a (simple) view with some text widgets, buttons and other widgets (see picture below). These widgets should be followed by a list of items. The whole view (except the app bar, of course) should be scrollable - not only the items.
That's why I put everything inside a ListView. But I'm not able to do something like this (while items
is a map with String values):
...
home: Scaffold(
appBar: AppBar(
title: Text('App bar'),
),
body: new ListView(
children: <Widget>[
new Text('Some Text'),
new FlatButton(...),
new Image.asset(...),
items.map((item)=>new Padding(
child: new Text(item.title),
)).toList()
],
),
),
...
What is the right way to get the desired view?
Thanks in advance!
You could use ListView.builder
since is more efficient, because the items are only created when they're scrolled onto the screen. And in the first position you could put the widgets that aren't from the list, like this:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('App bar')),
body: ListView.builder(
itemCount: items.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return Column(
children: <Widget>[
Text('Some Text'),
FlatButton(child: Text('A flat button'), onPressed: () {}),
Image.asset("An image"),
],
);
}
return Text('Item ${items[index].title}');
},
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With