gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:01:01 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何将 onTap 回调从一个类传递到另一个类。

我创建了一个用于构建列表的模型,在该模型中有一个 onTap 属性,我想做的是定义给您的回调,然后从 View 中访问点击。

持有回调的模型

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

列表定义示例

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(

最佳答案

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

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

typedef void MyCallback({String title});

关于flutter - 将回调从模型传递到 Flutter 中的 GestureDetector 的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50320176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com