I read the questions that were asked in this regard, but I did not get an answer or I made a mistake somewhere
According to the code I have, when clicking on each panel, the panels that were already open are not closed. And this is because I use one variable for all of them
How can this be solved?
bool isExpanded = false;
ListView.builder(
itemBuilder: (context, index) {
return Column(
children: [
ExpansionPanelList(
expansionCallback: (panelIndex, isExpanded) {
setState(() {
isExpandeder = !isExpanded;
});
},
children: [
ExpansionPanel(
headerBuilder: (context, isExpanded) {
return Row(
children: [
Text("Header"),
const Spacer(),
Text("username")
],
);
},
body: Column(
children: [
Text(
_signals![index].pair.toString(),
),
],
),
isExpanded: isExpanded,
),
],
)
],
);
},
itemCount: _signals!.length,
),
2
Answers
You already answered your question. You need a List of booleans or you add a isExpanded boolean in your signals object. If you have only variable, all the ExpansionPanels will behave the same.
You could do something like this:
So every expanded panel is collapsed at first time.
Now in your ExpansionPanel you just read the bool at position index:
And to switch your expansion panel state just invert the bool at the position in the isExpanded list:
You don’t need to have a list of bool values to store expanded values since you have only one value maximum. You can store index of currently expanded tile.
In your
ListView
you actually create multipleExpansionPanelList
s with one element each, instead of one list with all of them. You don’t need aListView
at all.You can try the following code in Dartpad, it does what you want.