我正在尝试自己学习Flutter,但是按钮的结构存在问题。
简单地说,我想在表中添加项目,用户可以从“可搜索的下拉菜单”中选择。
问题是我添加了表,而没有先从DropDown中进行选择。
List<Product> products;
List<Product> selectedProducts;
bool sort;
RelatedProductComboBox _selectedPlatformReach;
List<RelatedProductComboBox> _platformReach =
RelatedProductComboBox.getRelatedProduct();
List<DropdownMenuItem<RelatedProductComboBox>> _dropdownPlatformReach;
@override
void initState() {
sort = false;
selectedProducts = [];
products = Product.getProducts();
_dropdownPlatformReach =
buildDropdownMenuItemsRelatedProduct(_platformReach);
_selectedPlatformReach = _dropdownPlatformReach[0].value;
super.initState();
}
onSelectedRow(bool selected, Product product) async {
setState(() {
if (selected) {
selectedProducts.add(product);
} else {
selectedProducts.remove(product);
}
});
}
deleteSelected() async {
setState(() {
if (selectedProducts.isNotEmpty) {
List<Product> temp = [];
temp.addAll(selectedProducts);
for (Product product in temp) {
products.remove(product);
selectedProducts.remove(product);
}
}
});
}
SingleChildScrollView dataRelatedProduct() {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
DataTable(
sortAscending: sort,
sortColumnIndex: 0,
columns: [
DataColumn(
label: Text("Name"),
numeric: false,
),
],
rows: products
.map(
(product) => DataRow(
selected: selectedProducts.contains(product),
onSelectChanged: (b) {
print("Onselect");
onSelectedRow(b, product);
},
cells: [
DataCell(
SearchableDropdown(
isExpanded: true,
value: _selectedPlatformReach,
items: _dropdownPlatformReach,
isCaseSensitiveSearch: false,
onChanged:
(RelatedProductComboBox selectedPlatformReach) {
setState(() {
_selectedPlatformReach = selectedPlatformReach;
});
},
),
),
],
),
)
.toList(),
),
],
),
);
}