build method

  1. @override
Widget build(
  1. BuildContext context,
  2. WidgetRef ref
)

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context, WidgetRef ref) {
  ref.listen(updateProvider, (_, next) {
    if (next == UpdateInstallStatus.downloadComplete) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: const Text('A new version is ready.'),
          duration: const Duration(seconds: 10),
          action: SnackBarAction(
            label: 'Restart',
            onPressed: () =>
                ref.read(updateProvider.notifier).completeUpdate(),
          ),
        ),
      );
    }
  });
  final isOffline = ref.watch(isOfflineProvider);
  final isGuest = ref.watch(isAnonymousProvider);
  // Offer solo play only when a playable combination exists; an untimed mode
  // with a usable local bot, or a timed mode with a usable server bot (so the
  // name is "solo", not "local bots": both classes can fill the seats). See
  // [soloPlayAvailableProvider]. Most deployments with no bots get an empty
  // catalog → no extra FAB.
  final canPlaySolo = ref.watch(soloPlayAvailableProvider);
  final index = navigationShell.currentIndex;
  final branch = _ShellBranch.values[index];
  final title = branch.title;

  void selectBranch(int i) {
    navigationShell.goBranch(i, initialLocation: i == index);
  }

  void showNewGame() => showDialog<void>(
    context: context,
    useSafeArea: true,
    builder: (_) => const NewGameDialog(),
  );

  return AdaptiveLayoutBuilder(
    builder: (context, constraints, windowClass) {
      final compact = windowClass.isCompact;
      final expandedRail = windowClass.isAtLeastExpanded;
      final content = Column(
        children: [
          AnimatedSize(
            duration: const Duration(milliseconds: 200),
            curve: Curves.easeInOut,
            child: isOffline
                ? const _OfflineBanner()
                : const SizedBox.shrink(),
          ),
          Expanded(child: SafeArea(child: navigationShell)),
        ],
      );

      return Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: compact,
          title: title.isEmpty ? null : Text(title),
          actions: index == 0 && canPlaySolo
              ? [
                  IconButton(
                    onPressed: () => showDialog<void>(
                      context: context,
                      useSafeArea: true,
                      builder: (_) => const PlayVsBotDialog(),
                    ),
                    icon: const Icon(Icons.smart_toy_outlined),
                    tooltip: 'New Solo Game',
                  ),
                ]
              : null,
        ),
        drawer: compact
            ? NavigationDrawer(
                selectedIndex: index,
                onDestinationSelected: (int i) {
                  Navigator.of(context).pop();
                  selectBranch(i);
                },
                children: [
                  const _DrawerHeader(),
                  ..._drawerDestinations(isGuest: isGuest),
                  const _SignOutButton(),
                ],
              )
            : null,
        floatingActionButton: index == 0
            ? FloatingActionButton.extended(
                heroTag: 'newGame',
                onPressed: showNewGame,
                icon: const Icon(Icons.add),
                label: const Text('New Game'),
              )
            : null,
        body: compact
            ? content
            : Row(
                children: [
                  SafeArea(
                    right: false,
                    child: NavigationRail(
                      extended: expandedRail,
                      selectedIndex: index,
                      onDestinationSelected: selectBranch,
                      labelType: expandedRail
                          ? NavigationRailLabelType.none
                          : NavigationRailLabelType.all,
                      groupAlignment: -1,
                      scrollable: true,
                      leadingAtTop: false,
                      leading: expandedRail
                          ? const _RailHeader()
                          : const SizedBox(height: 16),
                      trailing: const _RailSignOutButton(),
                      trailingAtBottom: true,
                      destinations: _railDestinations(isGuest: isGuest),
                    ),
                  ),
                  const VerticalDivider(width: 1),
                  Expanded(child: content),
                ],
              ),
      );
    },
  );
}