我有一个字符串列表,它是一个文本,并且每个列表都应将我移动到特定的屏幕或操作,在遇到此问题时,如何为每个列表分配不同的路线?
这是代码:
List<String> items = [
'Spedizioni',
'I Tuoi ordini',
'Aggiungi un Prodotto',
'Contattaci'
];
child: Row(
children: [
Text(
items[i],
style: TextStyle(
color: _isHovering[i]
? Theme.of(context).primaryTextTheme.button.decorationColor
: Theme.of(context).primaryTextTheme.button.color,
),
),
SizedBox(width: 5,),
Icon(icons[i]),
]
),
);
直到这里一切正常,因为它在列表中显示了不同的项目,但是现在我希望列表中的每个项目都导航到一条路线,因为我现在只定义了一条,但是我需要为列表中的不同项目定义不同的路线onTap: () {
showDialog(
context: context,
builder: (context) => AddingProductsPopup(),
);
},
请帮我摆脱这个困
最佳答案
根据您的完整代码https://codeshare.io/an0jpv,您可以通过以下操作来实现:
onTap: () {
//here should have the options to call a different route for different List item menu
// call for our alert dialog widget including one more variable which is `items[i]` that has the text that would be passed to the alert
onPressed: () {
//here we show the pop up and pass the variable to it
showAlert(context, items[i]);
setState(() {});
},
现在让我们定义警报小部件showAlert(BuildContext context, String myItem) {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("this is the clicked item $myItem",);});
});
);
},
现在,当弹出窗口显示时,基于从items[i]
接收到的文本,对话框可以具有不同的标题,并且您可以添加不同的条件以根据从新闻项接收的文本来调整内容;
关于flutter - 为“字符串列表” flutter 中的每个元素定义导航路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64087035/