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.
diff --git a/packages/flutter/lib/src/painting/box_border.dart b/packages/flutter/lib/src/painting/box_border.dart
index e367d7a..375013b 100644
--- a/packages/flutter/lib/src/painting/box_border.dart
+++ b/packages/flutter/lib/src/painting/box_border.dart
@@ -314,6 +314,16 @@
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 @@
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 6cc94bf..7e7a42e 100644
--- a/packages/flutter/test/painting/border_test.dart
+++ b/packages/flutter/test/painting/border_test.dart
@@ -13,6 +13,16 @@
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);