Why isn't my DropdownButtonFormField showing the items? - flutter

I have this Dropdown:
DropdownButtonFormField(
value: shipmentSelected,
hint: Text(
'choose one',
),
onChanged: (value){
shipmentSelected = value;
}
items: product.shipment.map((Shipment shipment) {
return DropdownMenuItem(
value: shipment.code,
child: Text(
shipment.code,
),
);
}).toList(),
)
But it doesn't show any item... just show the hintText. How to fix it?

You need to pass a callback function to onChanged in the DropdownButtonFormField constructor. Update shipmentSelected and rebuild from this function like so:
onChanged: (value) {
setState(() {
shipmentSelected = value;
});
},

Related

How to not show the selected dropdown item in flutter

How to not show the selected item in the dropdown?
I m using DropdownButtonHideUnderline package in flutter to implement a dropdown but i don't want to show the selected item on the top.And i just need the selected item in a variable .How to do this?
Please help..
DropdownButtonHideUnderline(
child: DropdownButton2(
// Initial Value
value: dropdownValue,
// Down Arrow Icon
icon: const Icon(
Icons.more_vert,
color: Colors.white,
),
// Array list of items
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(
items,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
))
here you change the selected value:
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
}
by removing
setState(() {
dropdownValue = newValue!;
});
your dropdown will not change.
and you can use the chosen value by
onChanged: (String? newValue) => doSomething(newValue),
Future<void> doSomething(String? value) async {
...
}
You can ignore setting value on onChanged,
here, remove this, this is used to update the value on dropdownButton
.
onChanged: (String? newValue) {
// setState(() {
// dropdownValue = newValue!;
// });
},

Select Option not display new value after select change

I got issue to display value after select option... It always display Type All. I've follow from this answer but still got the problem.
DropdownButton(
hint: Text('Filter All Type'),
value: _typeDataL,
onChanged: (_newValue) {
setState(() {
_typeDataL = _newValue;
// print(typeDataL);
});
print(_newValue);
},
items: typeDataL.map((value) {
return DropdownMenuItem(
value: value,
child: new Text(value),
);
}).toList(),
),
try below code,
String? selectedValue = "Your init value";
var typeDataL = ["Your init value", "Option1", "Option2", "Option3"];
DropdownButton(
hint: const Text('Filter All Type'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue;
});
},
items: typeDataL.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
),
Your valueList should contain initial value, same as initial selectedValue and valueList should not contain duplicate values.

The argument type 'List<DropdownMenuItem<dynamic>>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?'

DropdownButton(
value: value,
items: locationItems,
onChanged: (value) => setState(() {
this.value = value;
print(value);
}),
),
for(int i = 0;i<widget.locationLength;i++){
locationItems.add(
DropdownMenuItem(
child: Text(
widget.locationName
),
value: "${widget.locationName}",
)
);
}
you need to add type to the dropdown button
DropdownButton<String>( value: value,
items: locationItems,
onChanged: (value) => setState(() {
this.value = value;
print(value);
}),
),
and you also must ensure that locationItems is of type List<DropdownMenuItem<String>>?. you can do this by adding type to DropdownMenuItem when adding to list like this:
locationItems.add( DropdownMenuItem<String>(
child: Text( widget.locationName ),
value: "${widget.locationName}",
)
);
or by strongly type locationItems when declaring it.
List<DropdownMenuItem<String>>? locationItems;

How to increase white space in between the text and the arrow in a DropdownButton in flutter?

DropdownButton(
value: dropDownValue1,
onChanged: (String? newValue) {
setState(() {
dropDownValue1 = newValue!;
});
},
items: <String>[
'Work At Office',
'Work From Home',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
how to increase space between the arrow and the text
I hope I can make it be like this, how to do it?
Thanks in advance
Just add isExpanded: true, under the dropdown for expanding icon
DropdownButton(
isExpanded: true, // here need to change
value: dropDownValue1,
onChanged: (String? newValue) {
setState(() {
dropDownValue1 = newValue!;
});
},
items: <String>[
'Work At Office',
'Work From Home',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
Output:
Try below code hope its helpful to you. refer my answer here also
Declare one variable for dropdown:
var dropdownValue = 'Work At Office';
Your widget
InputDecorator(
decoration: const InputDecoration(border: OutlineInputBorder()),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
isDense: true,
isExpanded: true,
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>[
'Work At Office',
'Work From Home',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),

Flutter DropdownButton widget with Getx

I'm in the process of learning GetX state management and stumble upon the DropdownButton widget. How do I update the selected value with the selected value cannot be observed. Here is my DropdownButton widget
DropdownButton(
hint: Text(
'Book Type',
),
onChanged: (newValue) {
print(newValue);
},
value: selectedType,
items: bookController.listType.map((selectedType) {
return DropdownMenuItem(
child: new Text(
selectedType,
),
value: selectedType,
);
}).toList(),
),
The
var selectedType;
declared in the widget build. I tried to make this variable observable but the layout throws an overflow error. I also wrap the widget with obx but still, it throws the same error. How do exactly this widget implement using GetX. I'm pulling my hair here. I can work with other widgets with getX.
First create your controller class.
class BookController extends GetxController {
// It is mandatory initialize with one value from listType
final selected = "some book type".obs;
void setSelected(String value){
selected.value = value;
}
}
On the view, instantiate your Controller and wrap the DropdownButton with an Obx widget:
BookController bookcontroller = BookController();
Obx( () => DropdownButton(
hint: Text(
'Book Type',
),
onChanged: (newValue) {
bookController.setSelected(newValue);
},
value: bookController.selected.value,
items: bookController.listType.map((selectedType) {
return DropdownMenuItem(
child: new Text(
selectedType,
),
value: selectedType,
);
}).toList(),
)
),
if you don't want to use observable variable then wrap your dropdown with getBuilder and in onChange function just update your controller like
onChanged: (newValue) {
bookController.currentDropdownValue=newValue;
bookController.update();
},
Example
//Controller
class HomeController extends GetxController {
var selectedDrowpdown = 'abc';
List dropdownText = ['abc', 'def', 'ghi'];
}
//dropdown button in Ui
DropdownButton(
hint: Text(
'Book Type',
),
onChanged: (newValue) {
homeController.selectedDrowpdown=newValue;
homeController.update();
},
value: homeController.selectedDrowpdown,
items: [
for (var data in homeController.dropdownTextList)
DropdownMenuItem(
child: new Text(
data,
),
value: data,
)
])
final selected = "".obs;
BookController bookcontroller = BookController();
Obx( () => DropdownButton(
hint: Text(
'Book Type',
),
onChanged: (newValue) {
bookController.setSelected(newValue);
},
value: bookController.selected.value==""?null:bookController.selected.value,
items: bookController.listType.map((selectedType) {
return DropdownMenuItem(
child: new Text(
selectedType,
),
value: selectedType,
);
}).toList(),
)
),
Try this, this worked for me.