flutter - 为“字符串列表” flutter 中的每个元素定义导航路线

标签 flutter dart flutter-layout

我有一个字符串列表,它是一个文本,并且每个列表都应将我移动到特定的屏幕或操作,在遇到此问题时,如何为每个列表分配不同的路线?
这是代码:

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/

相关文章:

flutter - 我收到错误 “Unable to load FastStringService”。我该如何解决?

flutter - SliverList flutter 中的分隔符/分隔符

firebase - 如何阅读 Firebase 文档

javascript - 是否可以在 Flutter 上使用 JavaScript 代替 Dart 来制作跨平台移动应用?

dart - 如何使用 dartanalyzer 忽略文件?

flutter - 如何更改 Flutter snackbar 的高度?

widget - 如何在 Card 对象上使用 Dismissible

angular - Dart,Angular 2 包/模块的条件加载

flutter - 将 ListTiles 排成一行

Flutter:是否可以在高度> 1.0 的文本行中设置垂直对齐方式?