From fef72c187de71d69eaefc60391fa13d8e1e1e748 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 3 Feb 2021 11:02:00 -0800 Subject: [PATCH] Added BorderRadius.copyWith (#75340) --- .../lib/src/painting/border_radius.dart | 16 +++++++++++++++ .../test/painting/border_radius_test.dart | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/flutter/lib/src/painting/border_radius.dart b/packages/flutter/lib/src/painting/border_radius.dart index 4e91bb100f688..f9f748b0ed1ed 100644 --- a/packages/flutter/lib/src/painting/border_radius.dart +++ b/packages/flutter/lib/src/painting/border_radius.dart @@ -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); diff --git a/packages/flutter/test/painting/border_radius_test.dart b/packages/flutter/test/painting/border_radius_test.dart index 7541e3a670361..6ca8244e7b925 100644 --- a/packages/flutter/test/painting/border_radius_test.dart +++ b/packages/flutter/test/painting/border_radius_test.dart @@ -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); + }); }