如何在flutter中创建普通参数类型的widget函数

How to create common parameter type widget function in flutter

我正在页面 class 函数中创建通用参数类型小部件函数。但需要创建用于多个class/page.

的公共参数类型widget函数
                  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)'.

我猜您在 dropdownBuilder 参数中使用了 _customDropDownProduct()。相反,您应该使用 _customDropDownProduct.

因此您不会传递函数的结果 (Widget),而是函数本身 Widget Function(BuildContext, Model, String).

要使其随处可用,您有两个选择:

  1. 提取_customDropDownProduct方法并使其成为全局函数
  2. 制作一个 CustomDropDownProduct 小部件并在您的不同屏幕上使用它。

这里是选项 2 的代码:

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)),
      ),
    );
  }
}

如何从dropdownBuilder调用它:

dropdownBuilder: (BuildContext context, ProductModel item, String itemDesignation) => 
    CustomDropDownProduct(
        item: item,
        itemDesignation: itemDesignation,
    ),