You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 15:54:03