How to use divideTiles() in Flutter? - flutter

I am trying to build a ListView with dividers between each ListTile.
I saw that there is a static method to do that called divideTiles() but i did not understand how to use that.. How/where is this function used?
My code is a simple ListView with ListTiles as children.

ListView(
children: ListTile.divideTiles(
context: context,
tiles: [
// your widgets here
]
).toList(),
)
Alternatively you can go with ListView.separated:
ListView.separated(
itemCount: 42,
itemBuilder: (context, index) {
// your widget here
},
separatorBuilder: (context, index) {
return Divider();
},
);

Here is a supplemental answer that shows what the divider looks like:
ListView(
children: ListTile.divideTiles(
context: context,
tiles: [
ListTile(
title: Text('Horse'),
),
ListTile(
title: Text('Cow'),
),
ListTile(
title: Text('Camel'),
),
ListTile(
title: Text('Sheep'),
),
ListTile(
title: Text('Goat'),
),
]
).toList(),
)
From here

If you are using listview you can go with this alternate
ListView.builder(itemBuilder: (context, index){
return ListTile(title: new Column(children: <Widget>[
new Text('Hello how are you?'),
new Divider(height: 20.0,)// add value for height or leave it blank for default
],) );
})

Related

How to implement scrollable FutureBuilder Flutter

How do i implement a scrollable futurebuilder?
I have this snippet. but rather than make it scroll up, it just does not scroll at all.
The snippet looks like this :
Center(
child: FutureBuilder<List<TransactionDetails>>(
future: fetchAlbum(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
child: Image.network(
snapshot.data![index].avatar.toString()),
),
title:
Text(snapshot.data![index].name.toString()),
trailing: Text(
snapshot.data![index].amount.toString()),
subtitle: Text(
snapshot.data![index].date.toString(),
style: TextStyle(fontSize: 9),
),
);
});
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
}))
How do I get it to do something like that scroll? Please I need some help here.
I assume you have enough data to scroll the view. in case you don't have enough data to scroll and want the scroll UX. i'll suggest you add the scroll physics to list view.
ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
shrinkWrap: true,
)
You can try wrapping the FutureBuilder() with a Column() because of your ListView.builder and then wrap the Column with a SingleChildScrollView(). The Column widget always tries to expand as big as it can, so you wrap it with a SingleChildScrollView to make it scrollable. The ListView.builder is already a scrollable by it self, but since you need to scroll the whole FutureBuilder you can do it this way. If you want to disable the list view inside the ListView.builder, you can use physics: NeverScrollableScrollPhysics().
SingleChildScrollView(
child: Column(children:[
Center(
child: FutureBuilder<List<TransactionDetails>>(
future: fetchAlbum(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
child: Image.network(
snapshot.data![index].avatar.toString()),
),
title:
Text(snapshot.data![index].name.toString()),
trailing: Text(
snapshot.data![index].amount.toString()),
subtitle: Text(
snapshot.data![index].date.toString(),
style: TextStyle(fontSize: 9),
),
);
});
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
}))
]),
),

What is wrong, I've used the relevant GetX widget and it still throws this error

This is the code where the error occurred using GetX. If anyone want to help thanks for advance
body: SafeArea(
child: Column(
children: [
Expanded(
child: GetX<CustomerController> (
builder: (customerController) {
return ListView.builder(
itemCount: customerController.count,
itemBuilder: (context, index) {
return ListTile(
selectedTileColor:Colors.blueGrey.shade50,
title: Text(customerController.results.string),
);
});
},
),
)
],
),
),
Create a GetX Controller and then assign it to your GetX Widget
GetX<ControllerName>(
builder: (customerController) {
return ListView.builder(
itemCount: customerController.controllervalueinobx.length, //should be obx/Rx type variable
itemBuilder: (context, index) {
return ListTile(
selectedTileColor: Colors.blueGrey.shade50,
title: Text(
"customerController.results.string",
), );
});
},
),

Flutter: how to delete item in ListTile

If i use Navigator, class is finished by itself.
I just want to delete the list.
code image is here
body: Container(
child: Center(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: launchButton('linkbutton','https://stackoverflow.com/questions/ask'),
trailing: OutlinedButton(
onPressed: () {
},
child: Icon(Icons.delete_forever),
Add an itemCount to your ListView.Builder.
Then if you want to delete the item at the given index, you can create a function and put it in your onpressed
deleteItem(int index){
yourList.removeWhere(index);
}

how to change body text ontap of a listview.builder item while im using map<>

I want to know how can i make items in a list view builder clickable so i can change text in the body (outside list view builder) because I am using a map<> data
ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
String key = values.keys.elementAt(index);
return new Column(
children: <Widget>[
new ListTile(
title: new Text("$key"),
subtitle: new Text("${values[key]}"),
),
new Divider(
height: 2.0,
),
],
);
},
);
new Text("${values[key]}"), i want to use this on body and change the text when i click on specific item .. thanks
You can use a variable to keep track on changes
1* declare variable
var myVar = "";
2* Edit your code
ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
String key = values.keys.elementAt(index);
return new GestureDetector(
onTap: (){
setState((){myVar = values[key]});},
child: Column(
children: <Widget>[
new ListTile(
title: new Text("$key"),
subtitle: new Text("${values[key]}"),
),
new Divider(
height: 2.0,
),
],
));
},
);
3* Then on body you can use it like this
Text(myVar)

Multiple listview.builder methods in single container

I have been trying to show few listviews in the same body container. I'm using Listview.builder because i have to fetch json data and display in a listview.
Each listview have to fetch data from different json files and display below the previous listview vertically(yes like in nested listview).
I have seen nested listvie examples but. Is it possible to do with listview.builder ? If so please show me example or a tutorial link. Thank You!
This is the code I'm use to create the listview.
ListView.builder(
itemCount: recent == null ? 0 : recent.length,
itemBuilder: (BuildContext context, int index) {
return Column(
children: <Widget>[
Card(
child: Column(
children: <Widget>[
new Image.network(recent[index]["_embedded"]["wp:featuredmedia"][0]["source_url"]),
new Padding(
padding: EdgeInsets.all(10.0),
child: new ListTile(
title: new Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: new Text(recent[index]["title"]["rendered"])),
subtitle: new Text(
recent[index]["excerpt"]["rendered"].replaceAll(new RegExp(r'<[^>]*>'), '')
),
),
)
],
),
)
],
);
},
)
);
You can achieve that by using SliverList with SliverChildBuilderDelegate:
CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
// First JSON
},
childCount: childCount,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
// Second JSON
},
childCount: childCount,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
// Third JSON
},
childCount: childCount,
),
),
),
],
);