在AppBar中颤动自定义操作 [英] Flutter custom actions in AppBar

查看:63
本文介绍了在AppBar中颤动自定义操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在整个应用程序中拥有相同的AppBar,我创建了一个单独的文件,其中包含要包含在每个屏幕中的AppBar.也可以传递一些变量,例如:action.因此,对于不同的屏幕,我仍可以在同一AppBar中进行不同的操作.所有这些工作正常,如下面的代码所示.但是,如果没有传递任何动作( dropdownChoices.length == 0 ),如何在AppBar中隐藏动作?

In order to have the same AppBar through my whole application, I created a separated file containing the AppBar which I include in every screen. It is also possible to pass some variables e.g.: actions. So for different screens I can have different actions still within the same AppBar. All this works fine like shown in the code below. But how to hide the Actions in the AppBar when there are no actions passed (dropdownChoices.length == 0)?

class BaseAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Color bgColor = HexToColor('#508bbb');
  final String title;
  final AppBar appBar;
  final TabBar tabBar;
  final List<DropdownChoices> dropdownChoices;
  final bool isHomepage;
  final bool goBack;

  final PageRouteBuilder _homeRoute = new PageRouteBuilder(
    pageBuilder: (BuildContext context, _, __) {
      return HomePage();
    },
  );

  BaseAppBar(
      {Key key,
      this.title,
      this.appBar,
      this.tabBar,
      this.dropdownChoices,
      this.isHomepage = false,
      this.goBack = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ...
      actions: <Widget>[
        PopupMenuButton<DropdownChoices>(
          onSelected: (value) {
            if (value.action == 'refresh') {
              Navigator.pushReplacementNamed(context, value.route);
            }
          },
          elevation: 6,
          itemBuilder: (BuildContext context) {
            return dropdownChoices.map((DropdownChoices choice) {
              return PopupMenuItem<DropdownChoices>(
                value: choice,
                child: Text(choice.title),
              );
            }).toList();
          },
        )
      ],
    );
  }
}

class DropdownChoices {
  const DropdownChoices({this.title, this.action, this.route});

  final String title;
  final String action;
  final String route;
}

推荐答案

我倾向于尝试类似

actions: (dropdownChoices.length < 1) ? null : <Widget>[
  PopupMenuButton<DropdownChoices>(...),
],

这篇关于在AppBar中颤动自定义操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆