buildPressable function

Widget buildPressable(
  1. BuildContext context,
  2. OpenUiNode node
)

Implementation

Widget buildPressable(BuildContext context, OpenUiNode node) {
  final theme = node.theme;
  final streaming = node.isStreaming;
  final selected = node.props.boolean('selected') == true;
  final radius = theme.radiusOf(node.props['radius']) ?? theme.radii.lg;
  final hasRow = node.props
      .elements('children')
      .any(
        (e) =>
            e.typeName == 'Row' ||
            (e.typeName == 'Box' && e.props.string('direction') == 'row'),
      );
  final background = node.props['background'];
  final on = theme.inkOn(background);
  final padding = theme.spaceOf(node.props.number('padding'));
  final shape = BorderRadius.circular(radius);
  final reduce = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
  final fade = reduce ? Duration.zero : const Duration(milliseconds: 120);
  Widget content = PressableScope(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      mainAxisSize: MainAxisSize.min,
      children: node.children(),
    ),
  );
  if (on != null) {
    content = OpenUiThemeScope(
      theme: theme.withInk(on),
      child: DefaultTextStyle.merge(
        style: TextStyle(color: on),
        child: content,
      ),
    );
  }
  final ring = selected && !hasRow;
  return ActionGate(
    node: node,
    prop: 'action',
    enabled: !streaming,
    label: _textOf,
    builder: (context, status, onTap) => Semantics(
      container: true,
      button: true,
      enabled: onTap != null,
      toggled: node.props['selected'] is bool ? selected : null,
      child: Press(
        radius: radius,
        onTap: onTap,
        builder: (hovered, pressed) {
          final scale = hasRow || reduce
              ? 1.0
              : (pressed ? 0.98 : (hovered ? 1.01 : 1.0));
          final overlay = pressed
              ? 0.08
              : hovered
              ? 0.05
              : (ring ? 0.06 : 0.0);
          final pending =
              status?.phase == ActionPhase.pending ||
              status?.phase == ActionPhase.unknown;
          return Opacity(
            opacity: pending ? 0.6 : 1,
            child: AnimatedScale(
              scale: scale,
              duration: fade,
              curve: Curves.easeOut,
              child: Stack(
                children: [
                  AnimatedContainer(
                    duration: fade,
                    curve: Curves.easeOut,
                    padding: padding == null ? null : EdgeInsets.all(padding),
                    decoration: BoxDecoration(
                      color: theme.bg(background),
                      gradient: theme.bgGradient(background),
                      borderRadius: shape,
                    ),
                    foregroundDecoration: BoxDecoration(
                      borderRadius: shape,
                      border: Border.all(
                        color: ring
                            ? theme.ink.withValues(alpha: 0.2)
                            : Colors.transparent,
                      ),
                    ),
                    child: content,
                  ),
                  Positioned.fill(
                    child: IgnorePointer(
                      child: AnimatedContainer(
                        duration: fade,
                        curve: Curves.easeOut,
                        decoration: BoxDecoration(
                          color: theme.ink.withValues(alpha: overlay),
                          borderRadius: shape,
                        ),
                      ),
                    ),
                  ),
                  if (ring)
                    PositionedDirectional(
                      top: 8,
                      end: 8,
                      child: Container(
                        width: 18,
                        height: 18,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: theme.ink,
                          shape: BoxShape.circle,
                        ),
                        child: CustomPaint(
                          size: const Size.square(12),
                          painter: _CheckMark(theme.checkInk),
                        ),
                      ),
                    ),
                ],
              ),
            ),
          );
        },
      ),
    ),
  );
}