我正在页面类函数内创建通用参数类型的小部件函数。但是需要创建用于多个类/页面的通用参数类型小部件函数。
DropdownSearch<ProductModel>(
searchBoxController: TextEditingController(text: ''),
mode: Mode.BOTTOM_SHEET,
isFilteredOnline: true,
showClearButton: true,
showSearchBox: true,
//label: 'Product Name *',
searchBoxDecoration: InputDecoration(
hintText:'Search Product',
contentPadding: EdgeInsets.fromLTRB(12, 12, 0, 0),
border: OutlineInputBorder()
),
dropdownSearchDecoration: InputDecoration(
hintText:'Select Product',
contentPadding: EdgeInsets.fromLTRB(5, 0, 0, 0),
hintStyle: TextStyle(fontSize: 12.0),
),
emptyBuilder:_customEmptyBuilderProduct,
selectedItem:jobLineList[index].productModel,
onFind: (String filter) => getProductData(filter),
dropdownBuilder: _customDropDownProduct,
popupItemBuilder: _customPopupItemBuilderProduct,
onChanged: (ProductModel value) {
print('onchanged');
print(value);
if(value == null) {
}
else {
}
},
),
_customDropDownProduct是下面显示的小部件函数,
Widget _customDropDownProduct( BuildContext context, ProductModel item, String itemDesignation) {
return Container(
//height: 20.0,
child: (item?.masterProductName == null)
? ListTile(
contentPadding: EdgeInsets.all(0),
//leading: CircleAvatar(),
title: Text("No product selected", style: TextStyle(fontSize: 12.0)),
)
: ListTile(
contentPadding: EdgeInsets.all(0),
title: Text(item.masterProductName, style: TextStyle(fontSize: 12.0)),
),
);
}
如何使_customDropDownProduct成为flutter中的常用函数。
出现类似的错误 The argument type 'Widget' can't be assigned to the parameter type 'Widget Function(BuildContext, Model, String)'.
我猜您_customDropDownProduct()
在dropdownBuilder参数中使用。相反,您应该使用_customDropDownProduct
。
因此,您不会传递函数(Widget
)的结果,而传递函数本身Widget Function(BuildContext, Model, String)
。
为了使它在任何地方都可用,您有两种选择:
_customDropDownProduct
方法并使其成为全局函数CustomDropDownProduct
小部件并在其他屏幕中使用它。对于选项2,代码如下:
CustomDropDownProduct类:
class CustomDropDownProduct extends StatelessWidget {
final ProductModel item;
final String itemDesignation;
CustomDropDownProduct({Key key, @required this.item, @required this.itemDesignation}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
//height: 20.0,
child: (item?.masterProductName == null)
? ListTile(
contentPadding: EdgeInsets.all(0),
//leading: CircleAvatar(),
title: Text("No product selected", style: TextStyle(fontSize: 12.0)),
)
: ListTile(
contentPadding: EdgeInsets.all(0),
title: Text(item.masterProductName, style: TextStyle(fontSize: 12.0)),
),
);
}
}
如何通过以下方式调用它dropdownBuilder
:
dropdownBuilder: (BuildContext context, ProductModel item, String itemDesignation) =>
CustomDropDownProduct(
item: item,
itemDesignation: itemDesignation,
),
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句