Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Extend EdgeInsetsGeometry with Utility Methods: add EdgeInsets.some and EdgeInsets.except constructors#176507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Mohamed-7018 wants to merge6 commits intoflutter:master
base:master
Choose a base branch
Loading
fromMohamed-7018:edge-insets-some-with-exceptSides
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
framework: add EdgeInsets.some and EdgeInsets.except* constructors
- Added `EdgeInsets.some` and `EdgeInsetsDirectional.some` for creating insets  where all sides share a default value, with optional overrides.- Added `EdgeInsets.exceptTop`, `EdgeInsets.exceptBottom`, `EdgeInsets.exceptLeft`,  and `EdgeInsets.exceptRight` for convenience in creating uniform insets except  for one side.- Added `EdgeInsetsDirectional.exceptStart` and `EdgeInsetsDirectional.exceptEnd`  for direction-aware insets with one side excluded.- Updated documentation for all new constructors.- Added comprehensive unit tests to cover `some` and `except*` constructors,  including LTR/RTL resolution.
  • Loading branch information
@Mohamed-7018
Mohamed-7018 committedOct 4, 2025
commit71a39e8b9e16206478517af16109f94ca8e05280
149 changes: 149 additions & 0 deletionspackages/flutter/lib/src/painting/edge_insets.dart
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,6 +40,22 @@ abstract class EdgeInsetsGeometry {
const factory EdgeInsetsGeometry.only({double left, double right, double top, double bottom}) =
EdgeInsets.only;

/// Creates [EdgeInsets] with all sides equal to [rest],
/// but allows overriding individual sides.
///
/// Example:
/// ```dart
/// EdgeInsetsGeometry.some(rest: 16, left: 8, top: 0);
/// ```
/// Results in: left = 8, top = 0, right = 16, bottom = 16
const factory EdgeInsetsGeometry.some({
double rest,
double? left,
double? top,
double? right,
double? bottom,
}) = EdgeInsets.some;

/// Creates [EdgeInsetsDirectional] with only the given values non-zero.
const factory EdgeInsetsGeometry.directional({
double start,
Expand DownExpand Up@@ -70,6 +86,20 @@ abstract class EdgeInsetsGeometry {
const factory EdgeInsetsGeometry.fromSTEB(double start, double top, double end, double bottom) =
EdgeInsetsDirectional.fromSTEB;

/// Creates insets where all sides are [value] except [top].
const factory EdgeInsetsGeometry.exceptTop(double value, [double top]) = EdgeInsets.exceptTop;

/// Creates insets where all sides are [value] except [bottom].
const factory EdgeInsetsGeometry.exceptBottom(double value, [double bottom]) =
EdgeInsets.exceptBottom;

/// Creates insets where all sides are [value] except [left].
const factory EdgeInsetsGeometry.exceptLeft(double value, [double left]) = EdgeInsets.exceptLeft;

/// Creates insets where all sides are [value] except [right].
const factory EdgeInsetsGeometry.exceptRight(double value, [double right]) =
EdgeInsets.exceptRight;

/// An [EdgeInsets] with zero offsets in each direction.
static const EdgeInsetsGeometry zero = EdgeInsets.zero;

Expand DownExpand Up@@ -431,6 +461,74 @@ class EdgeInsets extends EdgeInsetsGeometry {
right = horizontal,
bottom = vertical;

/// Creates insets where all sides are [value], except the excluded ones.
///
///{@tool snippet}
///
/// Example:
/// ```dart
/// const EdgeInsets.some(rest: 8.0, top: true); // -> left: 8, right: 8, bottom: 8, top: 0
/// ```
/// {@end-tool}
const EdgeInsets.some({
double rest = 0.0,
double? left,
double? top,
double? right,
double? bottom,
}) : left = left ?? rest,
top = top ?? rest,
right = right ?? rest,
bottom = bottom ?? rest;

/// Creates insets where all sides are set to [value] except the [top], which is
/// `0.0` by default.
///
/// {@tool snippet}
/// ```dart
/// // Creates insets: left=16, right=16, bottom=16, top=0
/// const EdgeInsets.exceptTop(16.0);
/// ```
/// {@end-tool}
const EdgeInsets.exceptTop(double value, [double top = 0.0])
: this.only(left: value, top: top, right: value, bottom: value);

/// Creates insets where all sides are set to [value] except the [bottom], which is
/// `0.0` by default.
///
/// {@tool snippet}
/// ```dart
/// // Creates insets: left=12, right=12, top=12, bottom=0
/// const EdgeInsets.exceptBottom(12.0);
/// ```
/// {@end-tool}
const EdgeInsets.exceptBottom(double value, [double bottom = 0.0])
: this.only(left: value, top: value, right: value, bottom: bottom);

/// Creates insets where all sides are set to [value] except the [left], which is
/// `0.0` by default.
///
/// {@tool snippet}
/// ```dart
/// // Creates insets: top=20, right=20, bottom=20, left=0
/// const EdgeInsets.exceptLeft(20.0);
/// ```
/// {@end-tool}
const EdgeInsets.exceptLeft(double value, [double left = 0.0])
: this.only(left: left, top: value, right: value, bottom: value);

/// Creates insets where all sides are set to [value] except the [right], which is
/// `0.0` by default.
///
/// {@tool snippet}
/// ```dart
/// // Creates insets: top=8, left=8, bottom=8, right=0
/// const EdgeInsets.exceptRight(8.0);
/// ```
/// {@end-tool}
const EdgeInsets.exceptRight(double value, [double right = 0.0])
: this.only(left: value, top: value, right: right, bottom: value);

/// Creates insets that match the given view padding.
///
/// If you need the current system padding or view insets in the context of a
Expand DownExpand Up@@ -760,6 +858,57 @@ class EdgeInsetsDirectional extends EdgeInsetsGeometry {
this.bottom = 0.0,
});

/// Creates [EdgeInsetsDirectional] where all sides default to [rest],
/// but allows overriding individual sides.
///
/// This is similar to [EdgeInsets.all], but with optional overrides.
///
/// For example:
/// ```dart
/// EdgeInsetsDirectional.some(rest : 20, top: 0, start: 8);
/// // -> start=8, top=0, end=20, bottom=20
/// ```
const EdgeInsetsDirectional.some({
double rest = 0.0,
double? start,
double? top,
double? end,
double? bottom,
}) : this.only(
start: start ?? rest,
top: top ?? rest,
end: end ?? rest,
bottom: bottom ?? rest,
);

/// Creates [EdgeInsetsDirectional] where all sides are [value],
/// except [start] which defaults to `0.0`.
///
/// This is useful when you want equal padding on all sides except
/// the `start` side in an LTR/RTL-aware layout.
///
/// For example:
/// ```dart
/// EdgeInsetsDirectional.exceptStart(16);
/// // -> start=0, top=16, end=16, bottom=16
/// ```
const EdgeInsetsDirectional.exceptStart(double value, [double start = 0.0])
: this.only(start: start, top: value, end: value, bottom: value);

/// Creates [EdgeInsetsDirectional] where all sides are [value],
/// except [end] which defaults to `0.0`.
///
/// This is useful when you want equal padding on all sides except
/// the `end` side in an LTR/RTL-aware layout.
///
/// For example:
/// ```dart
/// EdgeInsetsDirectional.exceptEnd(16);
/// // -> start=16, top=16, end=0, bottom=16
/// ```
const EdgeInsetsDirectional.exceptEnd(double value, [double end = 0.0])
: this.only(start: value, top: value, end: end, bottom: value);

/// Creates insets with symmetric vertical and horizontal offsets.
///
/// This is equivalent to [EdgeInsets.symmetric], since the inset is the same
Expand Down
125 changes: 125 additions & 0 deletionspackages/flutter/test/painting/edge_insets_test.dart
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,85 @@ void main() {
const EdgeInsets symmetric = EdgeInsets.symmetric(horizontal: 10, vertical: 20);
expect(symmetric.resolve(TextDirection.ltr), equals(const EdgeInsets.fromLTRB(10, 20, 10, 20)));
expect(symmetric.resolve(TextDirection.rtl), equals(const EdgeInsets.fromLTRB(10, 20, 10, 20)));

// some
const EdgeInsets some = EdgeInsets.some(rest: 10, top: 4);
expect(some.left, 10);
expect(some.right, 10);
expect(some.bottom, 10);
expect(some.top, 4);

const EdgeInsets some2 = EdgeInsets.some(rest: 16, top: 8);
const EdgeInsets someReference = EdgeInsets.only(left: 16, top: 8, right: 16, bottom: 16);
expect(some2, equals(someReference));

// Except-Top
const EdgeInsets exceptTop = EdgeInsets.exceptTop(12);
expect(exceptTop.left, 12);
expect(exceptTop.right, 12);
expect(exceptTop.bottom, 12);
expect(exceptTop.top, 0);

// Except-Top with value
const EdgeInsets exceptTopWithValue = EdgeInsets.exceptTop(20, 6);
const EdgeInsets expectedExceptTopWithValue = EdgeInsets.only(
left: 20,
top: 6,
right: 20,
bottom: 20,
);
expect(exceptTopWithValue, equals(expectedExceptTopWithValue));

// Except-Left
const EdgeInsets exceptLeft = EdgeInsets.exceptLeft(12);
expect(exceptLeft.top, 12);
expect(exceptLeft.right, 12);
expect(exceptLeft.bottom, 12);
expect(exceptLeft.left, 0);

// Except-Left with value
const EdgeInsets exceptLeftWithValue = EdgeInsets.exceptLeft(20, 6);
const EdgeInsets expectedExceptLeftWithValue = EdgeInsets.only(
top: 20,
left: 6,
right: 20,
bottom: 20,
);
expect(exceptLeftWithValue, equals(expectedExceptLeftWithValue));

// Except-Right
const EdgeInsets exceptRight = EdgeInsets.exceptRight(12);
expect(exceptRight.top, 12);
expect(exceptRight.left, 12);
expect(exceptRight.bottom, 12);
expect(exceptRight.right, 0);

// Except-Right with value
const EdgeInsets exceptRightWithValue = EdgeInsets.exceptRight(20, 6);
const EdgeInsets expectedExceptRightWithValue = EdgeInsets.only(
top: 20,
right: 6,
left: 20,
bottom: 20,
);
expect(exceptRightWithValue, equals(expectedExceptRightWithValue));

// Except-Bottom
const EdgeInsets exceptBottom = EdgeInsets.exceptBottom(12);
expect(exceptBottom.top, 12);
expect(exceptBottom.left, 12);
expect(exceptBottom.right, 12);
expect(exceptBottom.bottom, 0);

// Except-Bottom with value
const EdgeInsets exceptBottomWithValue = EdgeInsets.exceptBottom(20, 6);
const EdgeInsets expectedExceptBottomWithValue = EdgeInsets.only(
top: 20,
bottom: 6,
left: 20,
right: 20,
);
expect(exceptBottomWithValue, equals(expectedExceptBottomWithValue));
});

test('EdgeInsetsDirectional constructors', () {
Expand DownExpand Up@@ -66,6 +145,52 @@ void main() {
expect(all.resolve(TextDirection.ltr), equals(const EdgeInsets.fromLTRB(10, 10, 10, 10)));
expect(all.resolve(TextDirection.rtl), equals(const EdgeInsets.fromLTRB(10, 10, 10, 10)));

const EdgeInsetsDirectional insets1 = EdgeInsetsDirectional.some(rest: 16);
expect(insets1, const EdgeInsetsDirectional.only(start: 16, top: 16, end: 16, bottom: 16));

const EdgeInsetsDirectional insets2 = EdgeInsetsDirectional.some(rest: 20, top: 0, start: 8);
expect(insets2, const EdgeInsetsDirectional.only(start: 8, end: 20, bottom: 20));

const EdgeInsetsDirectional insetsExceptStart = EdgeInsetsDirectional.exceptStart(16);
expect(
insetsExceptStart,
const EdgeInsetsDirectional.only(top: 16, end: 16, bottom: 16),
);

const EdgeInsetsDirectional insetsExceptStart2 = EdgeInsetsDirectional.exceptStart(16, 4);
expect(
insetsExceptStart2,
const EdgeInsetsDirectional.only(start: 4, top: 16, end: 16, bottom: 16),
);

const EdgeInsetsDirectional insetsExceptStart3 = EdgeInsetsDirectional.exceptStart(16);
final EdgeInsets resolvedExceptStart3 = insetsExceptStart3.resolve(TextDirection.ltr);
expect(resolvedExceptStart3, const EdgeInsets.only(top: 16, right: 16, bottom: 16));

const EdgeInsetsDirectional insetsExceptStart4 = EdgeInsetsDirectional.exceptStart(16);
final EdgeInsets resolvedExceptStart4 = insetsExceptStart4.resolve(TextDirection.rtl);
expect(resolvedExceptStart4, const EdgeInsets.only(left: 16, top: 16, bottom: 16));

const EdgeInsetsDirectional insetsExceptEnd = EdgeInsetsDirectional.exceptEnd(16);
expect(
insetsExceptEnd,
const EdgeInsetsDirectional.only(start: 16, top: 16, bottom: 16),
);

const EdgeInsetsDirectional insetsExceptEnd2 = EdgeInsetsDirectional.exceptEnd(16, 6);
expect(
insetsExceptEnd2,
const EdgeInsetsDirectional.only(start: 16, top: 16, end: 6, bottom: 16),
);

const EdgeInsetsDirectional insetsExceptEnd3 = EdgeInsetsDirectional.exceptEnd(16);
final EdgeInsets resolvedExceptEnd3 = insetsExceptEnd3.resolve(TextDirection.ltr);
expect(resolvedExceptEnd3, const EdgeInsets.only(left: 16, top: 16, bottom: 16));

const EdgeInsetsDirectional insetsExceptEnd4 = EdgeInsetsDirectional.exceptEnd(16);
final EdgeInsets resolvedExceptEnd4 = insetsExceptEnd4.resolve(TextDirection.rtl);
expect(resolvedExceptEnd4, const EdgeInsets.only(top: 16, right: 16, bottom: 16));

// only
const EdgeInsetsDirectional directional = EdgeInsetsDirectional.only(
start: 10,
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp