Flutter 如何在ListView.builder中一次选择一个项目?
我有一个叫做情感的有状态小部件,它在下面显示一个图标和文本,图标变为绿色,文本在点击图标后显示。 我还有一个在屏幕容器中显示“情绪”的列表视图,如下所示:Flutter 如何在ListView.builder中一次选择一个项目?,flutter,dart,Flutter,Dart,我有一个叫做情感的有状态小部件,它在下面显示一个图标和文本,图标变为绿色,文本在点击图标后显示。 我还有一个在屏幕容器中显示“情绪”的列表视图,如下所示: Expanded( child: ListView.builder( itemExtent: 100, itemCount: moodIcons.length, shrinkWrap: tru
Expanded(
child: ListView.builder(
itemExtent: 100,
itemCount: moodIcons.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Emotions(
icon: moodIcons[index],
textIcon: textIcons[index],
);
}),
)
class Emotions extends StatefulWidget {
final IconData icon;
final String textIcon;
const Emotions({Key key, this.icon, this.textIcon}) : super(key: key);
@override
_EmotionsState createState() => _EmotionsState();
}
class _EmotionsState extends State<Emotions> {
bool _isPressed = false;
@override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: Duration(milliseconds: 350),
curve: Curves.easeIn,
child: Column(
children: [
GestureDetector(
onTap: () {
setState(() {
_isPressed = !_isPressed;
});
},
child: Icon(
widget.icon,
size: 40,
color: _isPressed ? Colors.green : Colors.black,
),
),
Text(
widget.textIcon,
style: TextStyle(
color: _isPressed
? Colors.black.withOpacity(0.7)
: Colors.black.withOpacity(0.0)),
),
],
),
);
}
}
扩展(
子项:ListView.builder(
项目范围:100,
itemCount:moodIcons.length,
收缩膜:对,
滚动方向:轴水平,
itemBuilder:(上下文,索引){
回归情感(
图标:moodIcons[索引],
文本图标:文本图标[索引],
);
}),
)
类扩展StatefulWidget{
最终的Iconda图标;
最终字符串文本图标;
常量情感({Key-Key,this.icon,this.textIcon}):super(Key:Key);
@凌驾
_情绪状态createState()=>_情绪状态();
}
类_EmotionsState扩展状态{
bool _isPressed=false;
@凌驾
小部件构建(构建上下文){
返回动画容器(
持续时间:持续时间(毫秒:350),
曲线:Curves.easeIn,
子:列(
儿童:[
手势检测器(
onTap:(){
设置状态(){
_isPressed=!\u isPressed;
});
},
子:图标(
widget.icon,
尺码:40,
颜色:_显示?颜色。绿色:颜色。黑色,
),
),
正文(
widget.textIcon,
样式:TextStyle(
颜色:\已显示
?颜色。黑色。不透明度(0.7)
:颜色。黑色。不透明度(0.0)),
),
],
),
);
}
}
我使用了以下两个列表:
List<IconData> moodIcons = <IconData>[
Icons.mood_bad,
Icons.sentiment_very_dissatisfied,
Icons.sentiment_dissatisfied,
Icons.sentiment_satisfied,
Icons.sentiment_very_satisfied
];
List<String> textIcons = <String>[
"bad mood",
"very sad",
"sad",
"happy",
"very happy"
];
List moodIcons=[
情绪不好,
你非常不满意,
你不满意,
你很满意,
你很满意
];
列表文本图标=[
“坏心情”,
“非常悲伤”,
“悲伤”,
“快乐”,
“非常高兴”
];
如何使此情感小部件列表一次只选择一个项目?如果您正在寻找嵌入到颤振框架中的答案;我不知道任何与ListView或其构造函数相关的内容。ListView小部件用于显示作为参数传递的子元素,而不需要更改这些元素中的状态 我的建议是使用与对象相同的元素列表,而不仅仅是普通字符串。在这些对象中,可以有字符串、图标和一个布尔属性,用于描述是否选中该项。点击后,更改列表以更新布尔属性属性上的各个项目,如果该列表处于该状态,它将刷新列表并仅显示一个选定项目 对象:
class MyObject {
IconData icon;
bool selected;
String text;
MyObject(this.icon, this.selected, this.text);
}
名单:
List moodIcons=[
MyObject(Icons.mood_bad,“坏心情”,false),
MyObject(图标、情绪、非常不满意、非常悲伤、错误),
];
ListView.builder:
ListView.builder(
itemCount: moodIcons.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
Color iconColor;
if(moodIcons.selected) {
iconColor = Colors.green;
} else {
iconColor = Colors.gray;
}
return GestureDetector(
child: Emotions(
icon: Icon(
moodIcons[index].icon,
color: iconColor,
),
textIcon: textIcons[index].text,
),
onTap: () {
for(int i = 0; i < moodIcons.length ; i++) {
if(i == index) {
moodIcons[i].selected = !moodIcons[i].selected;
} else if(moodIndex[i].selected) {
moodIcons[i].selected = false;
}
}
setState(() {});
}
);
}),
ListView.builder(
itemCount:moodIcons.length,
收缩膜:对,
滚动方向:轴水平,
itemBuilder:(上下文,索引){
彩色iconColor;
if(moodIcons.selected){
iconColor=Colors.green;
}否则{
iconColor=Colors.gray;
}
返回手势检测器(
孩子:情绪(
图标:图标(
moodIcons[index]。图标,
颜色:iconColor,
),
文本图标:文本图标[索引]。文本,
),
onTap:(){
for(int i=0;i
如果您正在寻找嵌入到颤振框架中的答案;我不知道任何与ListView或其构造函数相关的内容。ListView小部件用于显示作为参数传递的子元素,而不需要更改这些元素中的状态
我的建议是使用与对象相同的元素列表,而不仅仅是普通字符串。在这些对象中,可以有字符串、图标和一个布尔属性,用于描述是否选中该项。点击后,更改列表以更新布尔属性属性上的各个项目,如果该列表处于该状态,它将刷新列表并仅显示一个选定项目
对象:
class MyObject {
IconData icon;
bool selected;
String text;
MyObject(this.icon, this.selected, this.text);
}
名单:
List moodIcons=[
MyObject(Icons.mood_bad,“坏心情”,false),
MyObject(图标、情绪、非常不满意、非常悲伤、错误),
];
ListView.builder:
ListView.builder(
itemCount: moodIcons.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
Color iconColor;
if(moodIcons.selected) {
iconColor = Colors.green;
} else {
iconColor = Colors.gray;
}
return GestureDetector(
child: Emotions(
icon: Icon(
moodIcons[index].icon,
color: iconColor,
),
textIcon: textIcons[index].text,
),
onTap: () {
for(int i = 0; i < moodIcons.length ; i++) {
if(i == index) {
moodIcons[i].selected = !moodIcons[i].selected;
} else if(moodIndex[i].selected) {
moodIcons[i].selected = false;
}
}
setState(() {});
}
);
}),
ListView.builder(
itemCount:moodIcons.length,
收缩膜:对,
滚动方向:轴水平,
itemBuilder:(上下文,索引){
彩色iconColor;
if(moodIcons.selected){
iconColor=Colors.green;
}否则{
iconColor=Colors.gray;
}
返回手势检测器(
孩子:情绪(
图标:图标(
moodIcons[index]。图标,
颜色:iconColor,
),
文本图标:文本图标[索引]。文本,
),
onTap:(){
for(int i=0;i