getAppBarActions method

List<Widget>? getAppBarActions(
  1. BuildContext context
)

Implementation

List<Widget>? getAppBarActions(BuildContext context) {
  var list = (appBarButtons?.reversed.toList() ?? <Widget>[])
      .where((x) => (!(x is Container) || x.child != null))
      .toList()
      .whereNotNull();

  double titleWidth = MediaQuery.of(context).size.width;

  if (title is Text)
    titleWidth = (title as Text).getLayoutWidth(
      context,
      explicitTextStyle: TextStyles.textStyleSize(19),
    );
  if (title is SizedBox) titleWidth = (title as SizedBox).width ?? 50;

  if (description is Text)
    titleWidth = max(
        titleWidth,
        (description as Text).getLayoutWidth(
          context,
          explicitTextStyle: TextStyles.textStyleSize(13),
        ));
  if (description is SizedBox)
    titleWidth = max(titleWidth, (description as SizedBox).width ?? 50);

  var screenWidth = MediaQuery.of(context).size.width;
  var spaceForWidgets = max(
      0,
      screenWidth -
          titleWidth -
          45.0 -
          ((showBackArrow ?? Navigator.of(context).canPop()) ? 48 : 0));
  var maxWidgets = min(5, (spaceForWidgets / 48.0).floor());

  var newList = <Widget>[];
  var forMenu = <Widget>[];

  forMenu.addAll(appBarPopupButtons
          ?.where((x) => (!(x is Container) || x.child != null))
          .toList()
          .whereNotNull() ??
      <Widget>[]);

  if (forMenu.length > 0) maxWidgets--;

  if (list.length <= maxWidgets || list.length == 0) {
    newList.addAll(list);
  } else {
    newList
        .addAll(list.sublist(min(list.length, list.length - maxWidgets + 1)));
    forMenu.addAll(
        list.sublist(0, min(list.length, list.length - maxWidgets + 1)));
  }

  newList = newList.reversed.toList();

  if (forMenu.length > 0)
    newList.add(PopupMenuButton<void Function()>(
      icon: Icon(Icons.more_vert),
      onSelected: (value) => value.call(),
      tooltip: "Mehr zeigen",
      itemBuilder: (context) => forMenu
          .map(
            (item) {
              assert(
                  item is IconButton ||
                      item is PopupMenuEntry<void Function()> ||
                      item is PopupMenuButton<void Function()> ||
                      item is AppBarAction,
                  "Only IconButtons can be in the app bar when wrapping in a pop-up menu");

              if (item is PopupMenuEntry<void Function()>) {
                return item;
              }

              if (item is IconButton) {
                return PopupMenuItem(
                  value: item.onPressed,
                  child: IconTheme(
                    data: IconThemeData(size: 23, color: DefaultColors.text),
                    child: Row(
                      children: [
                        item.icon,
                        Container(width: 8),
                        Text(
                          item.tooltip!,
                          style: TextStyle(color: DefaultColors.text),
                        )
                      ],
                    ),
                  ),
                );
              }

              if (item is AppBarAction) {
                return PopupMenuItem<void Function()>(
                  value: (item as AppBarAction).onPressed,
                  child: IconTheme(
                    data: IconThemeData(size: 23, color: DefaultColors.text),
                    child: Row(
                      children: [
                        (item as AppBarAction).icon,
                        Container(width: 8),
                        Text(
                          (item as AppBarAction).tooltip,
                          style: TextStyle(color: DefaultColors.text),
                        )
                      ],
                    ),
                  ),
                );
              }

              if (item is PopupMenuButton<void Function()>) {
                return PopupMenuItem(
                  value: () {},
                  child: PopupMenuButton<void Function()>(
                    tooltip: item.tooltip,
                    child: Padding(
                      child: IconTheme(
                        data: IconThemeData(size: 23),
                        child: Row(
                          children: [
                            item.icon!,
                            Container(width: 8),
                            Text(item.tooltip!)
                          ],
                        ),
                      ),
                      padding: EdgeInsets.symmetric(vertical: 8.0),
                    ),
                    onSelected: item.onSelected,
                    itemBuilder: item.itemBuilder,
                  ),
                );
              }

              return PopupMenuItem<void Function()>(
                child: null,
              );
            },
          )
          .toList()
          .reversed
          .toList(),
    ));
  return newList;
}