From 0b8403b61c28ab33f1c93c7c3fdd463ff571acea Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Sun, 7 Apr 2019 21:29:32 -0700 Subject: [PATCH] Add `const Border.uniform()` (#30640) `Border.all()` is a factory constructor and thus not const constructible. This change adds a `const Border.uniform()` constructor and makes `Border.all()` delegate to it. This allows callers to more likely be able to make their widget tree const constructible. --- packages/flutter/lib/src/painting/box_border.dart | 12 +++++++++++- packages/flutter/test/painting/border_test.dart | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/painting/box_border.dart b/packages/flutter/lib/src/painting/box_border.dart index e367d7af2b..375013b4f7 100644 --- a/packages/flutter/lib/src/painting/box_border.dart +++ b/packages/flutter/lib/src/painting/box_border.dart @@ -314,6 +314,16 @@ class Border extends BoxBorder { assert(bottom != null), assert(left != null); + /// Creates a border whose sides are all the same. + /// + /// The `side` argument must not be null. + const Border.uniform(BorderSide side) + : assert(side != null), + top = side, + right = side, + bottom = side, + left = side; + /// A uniform border with all sides the same color and width. /// /// The sides default to black solid borders, one logical pixel wide. @@ -323,7 +333,7 @@ class Border extends BoxBorder { BorderStyle style = BorderStyle.solid, }) { final BorderSide side = BorderSide(color: color, width: width, style: style); - return Border(top: side, right: side, bottom: side, left: side); + return Border.uniform(side); } /// Creates a [Border] that represents the addition of the two given diff --git a/packages/flutter/test/painting/border_test.dart b/packages/flutter/test/painting/border_test.dart index 6cc94bf688..7e7a42e99b 100644 --- a/packages/flutter/test/painting/border_test.dart +++ b/packages/flutter/test/painting/border_test.dart @@ -13,6 +13,16 @@ void main() { expect(() => Border(bottom: nonconst(null)), throwsAssertionError); }); + test('Border.uniform constructor', () { + expect(() => Border.uniform(null), throwsAssertionError); + const BorderSide side = BorderSide(); + const Border border = Border.uniform(side); + expect(border.left, same(side)); + expect(border.top, same(side)); + expect(border.right, same(side)); + expect(border.bottom, same(side)); + }); + test('Border.merge', () { const BorderSide magenta3 = BorderSide(color: Color(0xFFFF00FF), width: 3.0); const BorderSide magenta6 = BorderSide(color: Color(0xFFFF00FF), width: 6.0);