build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Build a widget.

Implementation

@override
Widget build(BuildContext context) {
  final texts = widget.menuItems
      .map((menuItem) => Text(menuItem.title))
      .toList();
  final musicSound = widget.music;
  final child = GameShortcuts(
    shortcuts: [
      if (widget.canPop)
        GameShortcut(
          title: 'Close the menu',
          shortcut: widget.backShortcut,
          onStart: Navigator.maybePop,
        ),
      GameShortcut(
        title: 'Move up in the menu',
        shortcut: widget.upShortcut,
        onStart: (innerContext) => moveUp(),
      ),
      GameShortcut(
        title: 'Move down in the menu',
        shortcut: widget.downShortcut,
        onStart: (innerContext) => moveDown(),
      ),
      GameShortcut(
        title: 'Activate the current menu item',
        shortcut: widget.activateShortcut,
        onStart: activateItem,
      ),
      GameShortcut(
        title: 'Activate the current menu item',
        shortcut: GameShortcutsShortcut.space,
        onStart: activateItem,
      ),
    ],
    child: Material(
      child: OrientationBuilder(
        builder: (orientationContext, orientation) {
          final size = MediaQuery.of(orientationContext).size;
          final ruler = size.longestSide;
          return Semantics(
            excludeSemantics: true,
            label:
                'Swipe up and down to move through items in the menu or '
                'turn off your screen reader.',
            customSemanticsActions: {
              for (final menuItem in widget.menuItems)
                CustomSemanticsAction(label: menuItem.title): () {
                  orientationContext.maybePlaySound(widget.activateItemSound);
                  menuItem.onActivate(orientationContext);
                },
            },
            child: Stack(
              children: [
                switch (orientation) {
                  Orientation.portrait => Column(children: texts),
                  Orientation.landscape => Row(children: texts),
                },
                TouchMenuArea(
                  onDoubleTap: () => activateItem(orientationContext),
                  onPan: (point) {
                    final double coordinate;
                    switch (orientation) {
                      case Orientation.portrait:
                        coordinate = point.y;
                      case Orientation.landscape:
                        coordinate = point.x;
                    }
                    final scale = coordinate / ruler;
                    final index = (scale * (widget.menuItems.length - 1))
                        .round();
                    setCurrentMenuItem(index);
                  },
                ),
              ],
            ),
          );
        },
      ),
    ),
  );
  return ProtectSounds(
    sounds: [
      ...widget.menuItems.map((menuItem) => menuItem.earcon),
      widget.selectItemSound,
      widget.activateItemSound,
    ].whereType<Sound>().toList(),
    child: MaybeMusic(
      music: musicSound,
      fadeInTime: widget.musicFadeInTime,
      fadeOutTime: widget.musicFadeOutTime,
      error: widget.error,
      loading: () => child,
      child: Builder(builder: (context) => child),
    ),
  );
}