ListView.Builder播放/暂停按钮颤动 [英] ListView.Builder play/Pause buttom flutter
问题描述
我正在使用ListView.builder来创建音频列表,选择播放项时我需要将其单独更改为播放项,我尝试过bool,但播放或暂停时会更改列表中的所有项,有人可以帮忙吗?
I am using a ListView.builder to create an audio list, I need to change the play button pause individually to an item when I select it, I have tried a bool but giving play or pause changes all the items in the list, someone can you help with that ??
推荐答案
您可能有一个布尔列表来保存选择了哪个按钮,然后将布尔值作为参数传递给音频小部件,并使用bool更改图标.
You may have a list of boolean to save which button is selected, then pass the bool as a parameter to the audio widget, and use the bool to change the icon.
还传递回调函数来更改布尔列表,因为您必须从父窗口小部件中更改列表,因此需要回调函数.
Also pass a callback function to change the bool list, because you have to change the list from the parent widget, so a callback function is needed.
List<bool> audioSelectedList = List.generate(AudioList.length, (i) => false);
// This is a callback function that Audio will call when the button is clicked.
selected(int index){
// set only one bool to be true
setState(() {
audioSelectedList=List.generate(AudioList.length, (i) => false);// set all to false
audioSelectedList[index]=true; // set the selected index to be true
});
}
ListView:
ListView.builder(
itemCount: AudioList.length,
itemBuilder: (context, index) => Audio(
selected: selected, // pass the callback function
index: index, // use to call selected(index)
isSelected: audioSelectedList[index], // only one bool is true in the list which is the selected index.
),
),
这篇关于ListView.Builder播放/暂停按钮颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!