diff --git a/packages/flutter/lib/src/painting/edge_insets.dart b/packages/flutter/lib/src/painting/edge_insets.dart index 1a7756737627..5334913e965b 100644 --- a/packages/flutter/lib/src/painting/edge_insets.dart +++ b/packages/flutter/lib/src/painting/edge_insets.dart @@ -904,6 +904,22 @@ class EdgeInsetsDirectional extends EdgeInsetsGeometry { return EdgeInsets.fromLTRB(start, top, end, bottom); } } + + /// Creates a copy of this EdgeInsetsDirectional but with the given + /// fields replaced with the new values. + EdgeInsetsDirectional copyWith({ + double? start, + double? top, + double? end, + double? bottom, + }) { + return EdgeInsetsDirectional.only( + start: start ?? this.start, + top: top ?? this.top, + end: end ?? this.end, + bottom: bottom ?? this.bottom, + ); + } } class _MixedEdgeInsets extends EdgeInsetsGeometry { diff --git a/packages/flutter/test/painting/edge_insets_test.dart b/packages/flutter/test/painting/edge_insets_test.dart index 501f24a196db..b79759df16c4 100644 --- a/packages/flutter/test/painting/edge_insets_test.dart +++ b/packages/flutter/test/painting/edge_insets_test.dart @@ -279,4 +279,10 @@ void main() { expect(const EdgeInsetsDirectional.only(top: 4.0).add(const EdgeInsets.only(right: 3.0)).toString(), 'EdgeInsets(0.0, 4.0, 3.0, 0.0)'); expect(const EdgeInsetsDirectional.only(start: 4.0).add(const EdgeInsets.only(left: 3.0)).toString(), 'EdgeInsets(3.0, 0.0, 0.0, 0.0) + EdgeInsetsDirectional(4.0, 0.0, 0.0, 0.0)'); }); + + test('EdgeInsetsDirectional copyWith', () { + const EdgeInsetsDirectional sourceEdgeInsets = EdgeInsetsDirectional.only(start: 1.0, top: 2.0, bottom: 3.0, end: 4.0); + final EdgeInsetsDirectional copy = sourceEdgeInsets.copyWith(start: 5.0, top: 6.0); + expect(copy, const EdgeInsetsDirectional.only(start: 5.0, top: 6.0, bottom: 3.0, end: 4.0)); + }); }