首页 文章

从模型中将回调传递到Flutter中的GestureDetector的语法

提问于
浏览
0

我试图弄清楚如何将onTap回调从一个类传递到另一个类 .

我创建了一个用于构建列表的模型,在该模型中有一个onTap属性,我想要做的是定义哪个回调,然后从视图中访问 .

The Model which is to hold the callback

class MasterListsModel{
  final ListRowType rowType;
  final String assetName;
  final String name;
  final Color textColor;
  final Color backgroundColor;
  final void onTap; // <<------ The property that holds the callback

The list definition example

List<MasterListsModel> rows(Store<PrimeAppStateModel> store){
    List<MasterListsModel> list = [
      new MasterListsModel(
        ListRowType.headlinePrime,
        name: l10n.d1SearchHeader
      ),
      new MasterListsModel(ListRowType.icon,
        name: l10n.d1SearchMembers,
        textColor: hColors.white,
        assetName: hGraphics.segmentIconMembersWhite,
        backgroundColor: hColors.transparent,
        onTap: someVoidCallBackHere // <<----- The callback added to model
      ),

然后我想从我的列表构造类中唤起onTap .

return new Container(
      margin: new EdgeInsets.fromLTRB(35.0, 20.0, 0.0, 20.0),
      child: new GestureDetector(
        onTap: masterListModel.onTap, // << ----- Add callback
        child: new Row(
          children: <Widget>[
            new Container(

1 回答

  • 0

    使用 VoidCallback 而不是 void 类型在模型中声明回调 .

    您还可以使用 typedef 创建自己的回调类型

    typedef void MyCallback({String title});
    

相关问题