首先,我可能在设计解决方案上错了,所以可以提出完全不同的想法。
我的应用程序中有一个带有标签的页面。这些选项卡从公共类继承,因此它们可以具有与页面父级相似的界面。该页面上有一个应用程序栏,它当然可以保留操作。现在的问题是,对于其中一个标签,我想添加一个“编辑”操作,按下此按钮时,我将为该标签设置编辑模式。然后将操作更改为“保存”是有意义的。
我不确定在父页面没有该选项卡的自定义代码的情况下如何动态控制它。我只希望选项卡告诉父项它想要一个动作(我可以做),但是我不知道如何动态地执行操作(从“编辑”更改为“保存”),因为选项卡是StatefulWidget(仅具有最终成员) 。我不知道选项卡的状态如何以一种很好的方式将此通知父母。您怎么看?
这就是我的做法。
创建一个通知派生类:
class UpdateActionBarActionsNotification extends Notification {
final List<Widget> appBarActions;
UpdateActionBarActionsNotification({
this.appBarActions,
});
}
确保构建AppBar的小部件监听此通知:
NotificationListener<UpdateActionBarActionsNotification>(
onNotification: (notification) {
setState(() { _appBarActions = notification.appBarActions; });
return false;
},
child: ...
[_appBarActions
是要分配给AppBar的actions
属性的状态属性。
然后每个选项卡都可以使用此通知来显示其操作:
scheduleMicrotask(() {
UpdateActionBarActionsNotification(
appBarActions: [
...
],
).dispatch(context);
});
整个逻辑在https://github.com/nbonamy/stanley/blob/master/lib/tabs.dart中可用。示例(https://github.com/nbonamy/stanley/blob/master/example/lib/main.dart)显示了如何使用它。