我有一个 DropdownButton2 小部件,其中搜索基于两部分:DropdownMenuItem 的“子项”中空格之前的数字和空格之后的一些文本。在调试模式下,两个部分的搜索工作正常,但在发布模式下根本无法工作。
我想搜索第一部分和第二部分,即数字和文本。对于发布模式我需要添加什么吗?
请帮忙!
这是代码:
List<MyParts> myPartList = snapshot.data!;
List<DropdownMenuItem<String>> dropdownPartNumbers =
myPartList.map<DropdownMenuItem<String>>((item) {
String rawmatName = item.rawmatname!.length > 10
? "${item.rawmatname!.substring(0, 10)}..."
: item.rawmatname!;
return DropdownMenuItem<String>(
value: "${item.rawmatcode}-${item.serialNocapture}",
child: Text("${item.rawmatcode!} ($rawmatName)"),
);
}).toList();
// DropdownButton2 widget:
DropdownButtonHideUnderline(
child: DropdownButton2<String>(
isExpanded: true,
hint: Text(
'Select Part',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: dropdownPartNumbers,
value: _selectedValues[index],
onChanged: (value) {
setState(() {
_selectedValues[index] = value!;
if (value.contains("Y")) {
_qtyValues[index] = 1;
_isReadOnlyList[index] = true;
} else {
_isReadOnlyList[index] = false;
}
_isVisibleList[index] = value.contains("Y");
});
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
height: 60,
width: 200,
),
dropdownStyleData: const DropdownStyleData(
maxHeight: 350,
),
menuItemStyleData: const MenuItemStyleData(
height: 60,
),
dropdownSearchData: DropdownSearchData(
searchController: searchController,
searchInnerWidgetHeight: 100,
searchInnerWidget: Container(
height: 60,
padding: const EdgeInsets.only(
top: 8,
bottom: 4,
right: 8,
left: 8,
),
child: TextFormField(
expands: true,
maxLines: null,
controller: searchController,
decoration: InputDecoration(
isDense: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 8,
),
hintText: 'Search for part...',
hintStyle: const TextStyle(fontSize: 14),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
searchMatchFn: (item, searchValue) {
final lowerCaseValue =
searchValue.toLowerCase();
final parts = item.child
.toString()
.toLowerCase()
.split(" ");
final firstPart = parts[0];
final secondPart = parts.length > 1
? parts.sublist(1).join(" ")
: "";
return firstPart.contains(lowerCaseValue) ||
secondPart.contains(lowerCaseValue);
},
),
//This to clear the search value when you close the menu
onMenuStateChange: (isOpen) {
if (!isOpen) {
searchController.clear();
}
},
),
)
我找到了解决方案。搜索适用于 item.value,不适用于 item.child。我准备了如下所示的“DropdownMenuItem”:
DropdownMenuItem<String>(
value: "$rawmatCode-$serialNocapture-$rawmatName2",
child: Text("$rawmatCode ($rawmatName2)"),
);
}).toList();
搜索匹配功能的工作原理如下:
searchMatchFn: (item, searchValue) {
return item.value.toString().toLowerCase().contains(searchValue);
},