Skip to content

Commit

Permalink
Added BorderRadius.copyWith (flutter#75340)
Browse files Browse the repository at this point in the history
  • Loading branch information
HansMuller authored Feb 3, 2021
1 parent 3f163d2 commit fef72c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/flutter/lib/src/painting/border_radius.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ class BorderRadius extends BorderRadiusGeometry {
this.bottomRight = Radius.zero,
});

/// Returns a copy of this BorderRadius with the given fields replaced with
/// the new values.
BorderRadius copyWith({
Radius? topLeft,
Radius? topRight,
Radius? bottomLeft,
Radius? bottomRight,
}) {
return BorderRadius.only(
topLeft: topLeft ?? this.topLeft,
topRight: topRight ?? this.topRight,
bottomLeft: bottomLeft ?? this.bottomLeft,
bottomRight: bottomRight ?? this.bottomRight,
);
}

/// A border radius with all zero radii.
static const BorderRadius zero = BorderRadius.all(Radius.zero);

Expand Down
20 changes: 20 additions & 0 deletions packages/flutter/test/painting/border_radius_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,24 @@ void main() {
expect((a.add(b.subtract(a) * 0.0)).resolve(TextDirection.ltr), a);
expect((a.add(b.subtract(a) * 1.0)).resolve(TextDirection.rtl), b.resolve(TextDirection.rtl));
});

test('BorderRadius copyWith, merge, ==, hashCode basics', () {
const BorderRadius firstRadius = BorderRadius.all(Radius.circular(5.0));
final BorderRadius secondRadius = firstRadius.copyWith();
expect(firstRadius, secondRadius);
expect(firstRadius.hashCode, secondRadius.hashCode);
});

test('BorderRadius copyWith parameters', () {
const Radius radius = Radius.circular(10);
const BorderRadius borderRadius = BorderRadius.all(radius);
expect(borderRadius.copyWith(topLeft: Radius.zero).topLeft, Radius.zero);
expect(borderRadius.copyWith(topLeft: Radius.zero).copyWith(topLeft: radius), borderRadius);
expect(borderRadius.copyWith(topRight: Radius.zero).topRight, Radius.zero);
expect(borderRadius.copyWith(topRight: Radius.zero).copyWith(topRight: radius), borderRadius);
expect(borderRadius.copyWith(bottomLeft: Radius.zero).bottomLeft, Radius.zero);
expect(borderRadius.copyWith(bottomLeft: Radius.zero).copyWith(bottomLeft: radius), borderRadius);
expect(borderRadius.copyWith(bottomRight: Radius.zero).bottomRight, Radius.zero);
expect(borderRadius.copyWith(bottomRight: Radius.zero).copyWith(bottomRight: radius), borderRadius);
});
}

0 comments on commit fef72c1

Please sign in to comment.