skip to Main Content

Good time
I have two dropdowns on one page
The first is a list of cars and the second is the model of the cars.
Now, when I select Mercedes in the first drop-down menu, for example, I want the exact model name of the car brand to appear in the second drop-down menu.
For example, I choose Mercedes and in the next drop down, the list of Mercedes cars will be shown to me.
How can I handle it?

I am a newbie. And maybe it is better to give a more comprehensive explanation

2

Answers


  1. There are several ways to handle this.
    You can track the selected field, by adding it as a variable on your application, and everytime you select a new value, that variable is changed ex:

    manufacturer = "Mercedes"
    

    You can either have a variable (flag) in the builder that changes what is presented depending on its value,
    Ex: when a new value is selected on the first dropdown, you change the value of a variable, and the second dropdown loads a list based on that value.
    Ex:

    if (manufacturer == "Mercedes") { // load mercedes list on 2nd dropdown }
    

    or

    Create a state change where you load different values to the list depending on the selected value, with a stateChange of a StatefulWidget.

    Login or Signup to reply.
  2. Use Map to store the Manufacturer. For e.g. :

    Map<String,String> manufacturer = {'1':'BMW','2':'Toyota'};
    

    And also store list of cars using a map like this:

    Map<String,List<String>> cars = {
        '1':{'BMW car1','BMW car2'},
        '2':{'Toyota car1','Toyota car2'},
    };
    

    Then create a key variable which will stored key of selected brand.

    String key ='1';
    

    Then U can now choose the cars u desire using the key u chose for brand in dropdown.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search