build method

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

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) {
  final theme = Theme.of(context);
  final scaling = theme.scaling;
  final localizations = ShadcnLocalizations.of(context);
  var undoHistoryController = this.undoHistoryController;
  var contextMenuButtonItems =
      List.of(editableTextState.contextMenuButtonItems);

  ContextMenuButtonItem? take(ContextMenuButtonType type) {
    var item = contextMenuButtonItems
        .where((element) => element.type == type)
        .firstOrNull;
    if (item != null) {
      contextMenuButtonItems.remove(item);
    }
    return item;
  }

  var cutButton = take(ContextMenuButtonType.cut);
  var copyButton = take(ContextMenuButtonType.copy);
  var pasteButton = take(ContextMenuButtonType.paste);
  var selectAllButton = take(ContextMenuButtonType.selectAll);
  var shareButton = take(ContextMenuButtonType.share);
  var searchWebButton = take(ContextMenuButtonType.searchWeb);
  var liveTextInput = take(ContextMenuButtonType.liveTextInput);
  var cutButtonWidget = MenuButton(
    enabled: cutButton != null,
    onPressed: (context) {
      cutButton?.onPressed?.call();
    },
    trailing: const MenuShortcut(
      activator: SingleActivator(
        LogicalKeyboardKey.keyX,
        control: true,
      ),
    ),
    child: Text(localizations.menuCut),
  );
  var copyButtonWidget = MenuButton(
    enabled: copyButton != null,
    onPressed: (context) {
      copyButton?.onPressed?.call();
    },
    trailing: const MenuShortcut(
      activator: SingleActivator(
        LogicalKeyboardKey.keyC,
        control: true,
      ),
    ),
    child: Text(localizations.menuCopy),
  );
  var pasteButtonWidget = MenuButton(
    enabled: pasteButton != null,
    onPressed: (context) {
      pasteButton?.onPressed?.call();
    },
    trailing: const MenuShortcut(
      activator: SingleActivator(
        LogicalKeyboardKey.keyV,
        control: true,
      ),
    ),
    child: Text(localizations.menuPaste),
  );
  var selectAllButtonWidget = MenuButton(
    enabled: selectAllButton != null,
    onPressed: (context) {
      // somehow, we lost focus upon context menu open
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        selectAllButton?.onPressed?.call();
      });
    },
    trailing: const MenuShortcut(
      activator: SingleActivator(
        LogicalKeyboardKey.keyA,
        control: true,
      ),
    ),
    child: Text(localizations.menuSelectAll),
  );
  List<MenuItem> extras = [];
  if (shareButton != null) {
    extras.add(MenuButton(
      onPressed: (context) {
        shareButton.onPressed?.call();
      },
      child: Text(localizations.menuShare),
    ));
  }
  if (searchWebButton != null) {
    extras.add(MenuButton(
      onPressed: (context) {
        searchWebButton.onPressed?.call();
      },
      child: Text(localizations.menuSearchWeb),
    ));
  }
  if (liveTextInput != null) {
    extras.add(MenuButton(
      onPressed: (context) {
        liveTextInput.onPressed?.call();
      },
      child: Text(localizations.menuLiveTextInput),
    ));
  }
  if (undoHistoryController == null) {
    return TextFieldTapRegion(
      child: ShadcnUI(
        child: ContextMenuPopup(
          anchorSize: Size.zero,
          anchorContext: anchorContext,
          position: editableTextState.contextMenuAnchors.primaryAnchor +
              const Offset(8, -8) * scaling,
          children: [
            cutButtonWidget,
            copyButtonWidget,
            pasteButtonWidget,
            selectAllButtonWidget,
            if (extras.isNotEmpty) const MenuDivider(),
            ...extras,
          ],
        ),
      ),
    );
  }
  return TextFieldTapRegion(
    child: ShadcnUI(
      child: AnimatedBuilder(
          animation: undoHistoryController,
          builder: (context, child) {
            return ContextMenuPopup(
              anchorContext: anchorContext,
              position: editableTextState.contextMenuAnchors.primaryAnchor +
                  const Offset(8, -8) * scaling,
              children: [
                MenuButton(
                  enabled: undoHistoryController.value.canUndo,
                  onPressed: (context) {
                    undoHistoryController.undo();
                  },
                  trailing: const MenuShortcut(
                    activator: SingleActivator(
                      LogicalKeyboardKey.keyZ,
                      control: true,
                    ),
                  ),
                  child: const Text('Undo'),
                ),
                MenuButton(
                  enabled: undoHistoryController.value.canRedo,
                  onPressed: (context) {
                    undoHistoryController.redo();
                  },
                  trailing: const MenuShortcut(
                    activator: SingleActivator(
                      LogicalKeyboardKey.keyZ,
                      control: true,
                      shift: true,
                    ),
                  ),
                  child: const Text('Redo'),
                ),
                const MenuDivider(),
                cutButtonWidget,
                copyButtonWidget,
                pasteButtonWidget,
                selectAllButtonWidget,
                if (extras.isNotEmpty) const MenuDivider(),
                ...extras,
              ],
            );
          }),
    ),
  );
}