Widget _buildEquipmentCategoriesDropdown() {
return Column(
//
children: [
Align(
alignment: Alignment.centerLeft,
child: RichText(
text: TextSpan(text: 'Equipment Categories: ', style: Styles.blackBold16, children: [
TextSpan(
text: '*',
style: TextStyle(
color: ColorsValue.orangeColor,
fontWeight: FontWeight.bold,
),
),
]),
),
),
Dimens.boxHeight5,
Container(
padding: EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black26,
offset: const Offset(
5.0,
5.0,
),
blurRadius: 5.0,
spreadRadius: 1.0,
),
BoxShadow(
color: ColorsValue.whiteColor,
offset: const Offset(0.0, 0.0),
blurRadius: 0.0,
spreadRadius: 0.0,
),
],
color: ColorsValue.whiteColor,
borderRadius: BorderRadius.circular(5),
),
child: //
Obx(
() => MultiSelectDialogField(
initialValue:
//
controller.selectedEquipmentCategoryList,
decoration: BoxDecoration(border: Border()),
buttonIcon: Icon(Icons.arrow_drop_down),
items: controller.equipmentCategoryList
.map(
(equipmentCategory) => MultiSelectItem(
equipmentCategory?.id,
equipmentCategory?.name ?? '',
),
)
.toList(),
onConfirm: (selectedOptionsList) => {controller.equipmentCategoriesSelected(selectedOptionsList)},
chipDisplay: MultiSelectChipDisplay(),
),
),
),
Dimens.boxHeight20,
],
);
}
Controller:
Future<void> getInventoryCategoryList(String? facilityId) async {
//// SEE HERE, THE DATA TYPE FOR BOTH THE LIST AND THE INITIAL VALUE IS THE SAME
equipmentCategoryList.value = <InventoryCategoryModel>[];
selectedEquipmentCategoryList.value = <InventoryCategoryModel>[];
selectedEquipmentCategoryNameList.value = <String>[];
//
final _equipmentCategoryList = await editJobPresenter.getInventoryCategoryList(
isLoading: true,
);
if (_equipmentCategoryList != null) {
for (var equimentCategory in _equipmentCategoryList) {
equipmentCategoryList.add(equimentCategory);
}
//selectedEquipmentCategoryList = equipmentCategoryList;
if (jobDetailsModel.value?.equipmentCatList != null)
for (var equipCat in jobDetailsModel.value?.equipmentCatList ?? []) {
InventoryCategoryModel equipmentCategory = InventoryCategoryModel(
id: equipCat.equipmentCatId,
name: equipCat.equipmentCatName,
);
selectedEquipmentCategoryList.add(equipmentCategory);
print(selectedEquipmentCategoryList[0]);
selectedEquipmentCategoryNameList.add(equipmentCategory.name);
}
}
}
I want to have certain values pre-filled in the multiselect, the widget just won’t do it.
The data for the list (initial values) comes from an API and then the UI gets updated (using Getx).
6
Answers
The default value of the select element can be set by using the ‘selected’ attribute on the required option. This is a boolean attribute. The option that is having the ‘selected’ attribute will be displayed by default on the dropdown list
When you use a MultiSelectDialogField in Flutter, you can set an initial value that is pre-selected when the dialog opens. To set this initial value, you need to provide a list of items that you want to be selected initially.
In the given code, the initialValue parameter of the MultiSelectDialogField is set to [controller.selectedEquipmentCategoryList]. This means that the list of selected equipment categories stored in the controller.selectedEquipmentCategoryList variable will be used as the initial value for the dropdown.
So when the user opens the dropdown, the items in the controller.selectedEquipmentCategoryList will already be selected, and the user can add or remove other items as desired.
I think you are also confusing Rx as an ObserVER vs. ObservABLE. Rx is an observable, i.e. you watch it for changes using Obx or GetX widgets, (I guess you can call these two widgets "Observers".)
Example
Hi i hope below answer will help you. you should use
GetBuilder
instead of Obx and you must initialize controller call Api using load controller.in this example, i am ignoring your predefined decoration variable and colors,
Controller code is below
if you want equipment category should load first, you dont want to show empty list then call
controller.loadequipment();
it on splash screen or home screen.your init(selected) List and List from APi maybe the same values but they have different InventoryCategoryModel object instances so that can possibly be causing the problem with the ==(equality) check of the InventoryCategoryModel model.
try to adding this override for == operator in your model.
Try this,
here I am passing " InventoryCategoryModel" items in initial values,its work