Flutter中返回List<Widget>的方法如何为AppBar actions提供两个操作按钮
解决方法
你定义的方法要求返回List<Widget>类型,当前仅创建了两个IconButton实例,没有组装为列表返回,注释位置直接返回包含这两个按钮的列表即可,完整修正代码如下:
List<Widget> buildViewingActions(BuildContext context, Event event){ return [ IconButton( icon: const Icon(Icons.edit), onPressed: (){ Get.to(EventCreatingPage(event: event,)); }, ), IconButton( icon: const Icon(Icons.delete), onPressed: (){ Get.offAll(CalendarPage()); }, ), ]; }
可选写法
你也可以先声明空列表逐个添加组件后再返回,效果完全一致:
List<Widget> buildViewingActions(BuildContext context, Event event){ List<Widget> actionList = []; actionList.add( IconButton( icon: const Icon(Icons.edit), onPressed: (){ Get.to(EventCreatingPage(event: event,)); }, ) ); actionList.add( IconButton( icon: const Icon(Icons.delete), onPressed: (){ Get.offAll(CalendarPage()); }, ) ); return actionList; }
之后你直接在AppBar的actions参数中调用这个方法就可以正常显示两个操作按钮了。
内容的提问来源于stack exchange,提问作者matti peterbull
相关产品推荐
相关产品推荐

