Reputation: 53
I am creating common parameter type widget function within page class function. But need to create common parameter type widget function which is used to more than one class/page.
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 is widget function show below,
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)),
),
);
}
How to make _customDropDownProduct as common function in flutter.
Getting error like
The argument type 'Widget' can't be assigned to the parameter type 'Widget Function(BuildContext, Model, String)'.
Upvotes: 0
Views: 679
Reputation: 3594
I guess you are using _customDropDownProduct()
in a dropdownBuilder argument. Instead you should use _customDropDownProduct
.
Therefore you won't pass the result of the function (Widget
) but the function itself Widget Function(BuildContext, Model, String)
.
To make it available everywhere you have two options:
_customDropDownProduct
method and make it a global functionCustomDropDownProduct
widget and use it in your different screen.For option 2 here is the code:
The CustomDropDownProduct class:
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)),
),
);
}
}
How to call it from dropdownBuilder
:
dropdownBuilder: (BuildContext context, ProductModel item, String itemDesignation) =>
CustomDropDownProduct(
item: item,
itemDesignation: itemDesignation,
),
Upvotes: 1