Search code examples
flutterdartflutter-layout

How do I convert a DropdownButton widget into a listview widget in flutter


I have a dropdown that shows products based on category clicked and I want to convert into a listview but I don’t know to go about it. Is there anyway I can change it into a horizontal listview

Here’s the code:

DropdownButton<CategoryData>(
value: getCategories.selectedCategory,
onChanged: (CategoryData? newValue) {
setState(() {});
},
item: getCategories.allCategories.value.data!.map(CategoryData value) {
return DropdownMenuItem<CategoryData>(
value: value,
child: Row()

Solution

  • If you don't explicitly need a dropdown there you can use Listview of ExpansionTiles and make create a ListView passed to it's children parameter. This way you'd have a product tree that grows with each selection.

    If dropdown is a must I'd recommend creating a variable populated with selected kind of products, then in build tree simply use function returning Widget based on previously mentioned variable this way you have a dropdown that is being switched to a Listview of selected category.

    Function would look like:

    Widget dropdownOrListView() {
    if (categorySelected) {
    return Listview
    } else {
    return dropdown
    }
    }
    

    Hope this helps. If I didn't understand the question correctly feel free to edit post or comment with additional information.