buildByMaterialWithChild method

Widget buildByMaterialWithChild(
  1. Widget child, [
  2. BuildContext? context
])

Final build method, for compatibility with BaseButton.icon

Implementation

Widget buildByMaterialWithChild(Widget child, [BuildContext? context]) {
  // Ensure only one button type is selected at a time
  final List<bool> buttonTypes = [textButton, outlinedButton, elevatedButton, filledButton, filledTonalButton];
  final int selectedTypes = buttonTypes.where((type) => type).length;
  assert(
    selectedTypes <= 1,
    'Only one button type can be true at a time: textButton, outlinedButton, elevatedButton, filledButton, or filledTonalButton.',
  );
  assert(child != null, 'child can\'t be null.');

  final VoidCallback? _onPressed = valueOf('onPressed', onPressed);
  final VoidCallback? _onLongPress = valueOf('onLongPress', onLongPress);
  final ButtonStyle? _style = valueOf('style', style);
  final FocusNode? _focusNode = valueOf('focusNode', focusNode);
  final bool _autofocus = valueOf('autofocus', autofocus);
  final Clip _clipBehavior = valueOf('clipBehavior', clipBehavior);

  // iOS 26 Features (adaptive haptics work on Material too)
  final bool _adaptiveHaptics = valueOf('adaptiveHaptics', adaptiveHaptics);

  // Enhanced onPressed with haptic feedback
  VoidCallback? _enhancedOnPressed;
  if (_onPressed != null) {
    _enhancedOnPressed = () {
      if (_adaptiveHaptics) {
        HapticFeedback.mediumImpact(); // Slightly stronger feedback for Material
      }
      _onPressed();
    };
  }

  // Material 3 enhanced button styling (only if context is available)
  ButtonStyle? enhancedStyle = _style;
  if (context != null) {
    final ThemeData theme = Theme.of(context);
    final ColorScheme colorScheme = theme.colorScheme;

    // Enhanced ButtonStyle for Material 3 compliance
    if (theme.useMaterial3 && enhancedStyle == null) {
      enhancedStyle = ButtonStyle(
        elevation: MaterialStateProperty.resolveWith<double>((states) {
          if (states.contains(MaterialState.disabled)) return 0;
          if (states.contains(MaterialState.pressed)) return 1;
          return 2;
        }),
        shadowColor: MaterialStateProperty.all(colorScheme.shadow),
        surfaceTintColor: MaterialStateProperty.all(colorScheme.surfaceTint),
      );
    }
  }

  if (textButton) {
    return TextButton(
      onPressed: _enhancedOnPressed,
      onLongPress: _onLongPress,
      style: enhancedStyle,
      focusNode: _focusNode,
      autofocus: _autofocus,
      clipBehavior: _clipBehavior,
      child: child,
    );
  } else if (outlinedButton) {
    return OutlinedButton(
      onPressed: _enhancedOnPressed,
      onLongPress: _onLongPress,
      style: enhancedStyle,
      focusNode: _focusNode,
      autofocus: _autofocus,
      clipBehavior: _clipBehavior,
      child: child,
    );
  } else if (elevatedButton) {
    return ElevatedButton(
      onPressed: _enhancedOnPressed,
      onLongPress: _onLongPress,
      style: enhancedStyle,
      focusNode: _focusNode,
      autofocus: _autofocus,
      clipBehavior: _clipBehavior,
      child: child,
    );
  } else if (filledButton) {
    return FilledButton(
      onPressed: _enhancedOnPressed,
      onLongPress: _onLongPress,
      style: enhancedStyle,
      focusNode: _focusNode,
      autofocus: _autofocus,
      clipBehavior: _clipBehavior,
      child: child,
    );
  } else if (filledTonalButton) {
    return FilledButton.tonal(
      onPressed: _enhancedOnPressed,
      onLongPress: _onLongPress,
      style: enhancedStyle,
      focusNode: _focusNode,
      autofocus: _autofocus,
      clipBehavior: _clipBehavior,
      child: child,
    );
  }

  // Fallback to MaterialButton for legacy support
  return MaterialButton(
    onPressed: _enhancedOnPressed,
    onLongPress: _onLongPress,
    onHighlightChanged: valueOf('onHighlightChanged', onHighlightChanged),
    mouseCursor: valueOf('mouseCursor', mouseCursor),
    textTheme: valueOf('textTheme', textTheme),
    textColor: valueOf('textColor', textColor),
    disabledTextColor: valueOf('disabledTextColor', disabledTextColor),
    color: valueOf('color', color),
    disabledColor: valueOf('disabledColor', disabledColor),
    focusColor: valueOf('focusColor', focusColor),
    hoverColor: valueOf('hoverColor', hoverColor),
    highlightColor: valueOf('highlightColor', highlightColor),
    splashColor: valueOf('splashColor', splashColor),
    colorBrightness: valueOf('colorBrightness', colorBrightness),
    elevation: valueOf('elevation', elevation),
    focusElevation: valueOf('focusElevation', focusElevation),
    hoverElevation: valueOf('hoverElevation', hoverElevation),
    highlightElevation: valueOf('highlightElevation', highlightElevation),
    disabledElevation: valueOf('disabledElevation', disabledElevation),
    padding: valueOf('padding', padding),
    visualDensity: valueOf('visualDensity', visualDensity),
    shape: valueOf('shape', shape),
    clipBehavior: _clipBehavior,
    focusNode: _focusNode,
    autofocus: _autofocus,
    materialTapTargetSize: valueOf('materialTapTargetSize', materialTapTargetSize),
    animationDuration: valueOf('animationDuration', animationDuration),
    minWidth: valueOf('minWidth', minWidth),
    height: valueOf('height', height),
    enableFeedback: valueOf('enableFeedback', enableFeedback),
    child: child,
  );
}