withPadding method

Widget withPadding({
  1. double? top,
  2. double? bottom,
  3. double? left,
  4. double? right,
  5. double? horizontal,
  6. double? vertical,
  7. double? all,
  8. EdgeInsetsGeometry? padding,
})

Wraps this widget in a Padding widget.

Provides flexible padding specification via individual edges, combined directions, or uniform padding.

Parameters:

  • top (double?, optional): Top padding.
  • bottom (double?, optional): Bottom padding.
  • left (double?, optional): Left padding.
  • right (double?, optional): Right padding.
  • horizontal (double?, optional): Left and right padding (cannot use with left/right).
  • vertical (double?, optional): Top and bottom padding (cannot use with top/bottom).
  • all (double?, optional): Uniform padding on all sides (cannot use with others).
  • padding (EdgeInsetsGeometry?, optional): Direct padding value (overrides all other params).

Returns: Widget — padded widget.

Throws FlutterError if conflicting parameters are used.

Implementation

Widget withPadding(
    {double? top,
    double? bottom,
    double? left,
    double? right,
    double? horizontal,
    double? vertical,
    double? all,
    EdgeInsetsGeometry? padding}) {
  assert(() {
    if (all != null) {
      if (top != null ||
          bottom != null ||
          left != null ||
          right != null ||
          horizontal != null ||
          vertical != null) {
        throw FlutterError(
            'All padding properties cannot be used with other padding properties.');
      }
    } else if (horizontal != null) {
      if (left != null || right != null) {
        throw FlutterError(
            'Horizontal padding cannot be used with left or right padding.');
      }
    } else if (vertical != null) {
      if (top != null || bottom != null) {
        throw FlutterError(
            'Vertical padding cannot be used with top or bottom padding.');
      }
    }
    return true;
  }());
  var edgeInsets = EdgeInsets.only(
    top: top ?? vertical ?? all ?? 0,
    bottom: bottom ?? vertical ?? all ?? 0,
    left: left ?? horizontal ?? all ?? 0,
    right: right ?? horizontal ?? all ?? 0,
  );
  return Padding(
    padding: padding ?? edgeInsets,
    child: this,
  );
}