Define navigation route for each element in a String List flutter

382 views Asked by At

I have a list of string which is a Text and each of this List should move me to a specific screen or action, at the momnent I am stuck into this issue, how to assign to each of the list a different route? this is the code:

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]),
          ]
        ),
      );

and until here all ok because it show different item into the list but now I want each of this item in the list navigate to a route as now I defined just one, but I need to define different route for different item in the List

onTap: () {
          showDialog(
            context: context,
            builder: (context) => AddingProductsPopup(),
          );
        },

Please help me to go out from this stuck

1

There are 1 answers

3
Asmoun On BEST ANSWER

based on your full code https://codeshare.io/an0jpv you can achieve that by like following :

     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(() {});
            },

now let's define our alert widget

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",);});
            });
          );
        },

now when the popup shows and based on the received text from items[i] the dialog can have deferent titles , and you can add deferent conditions to adjust the content based on the text received from the press item;