From 34656ff29acd12e8af2bd055eb716ba1271dc71e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Mon, 2 Jun 2025 16:09:05 -0700 Subject: [PATCH] Add convenience static members to AlignmentGeometry (#169709) This adds convenience members to `AlignmentGeometry` in preparation for Dart shorthands. Before: ```dart Container( alignment: Alignment.topLeft, child: Text('Hello world'), ), ``` After (with Dart shorthands): ```dart Container( alignment: .topLeft, child: Text('Hello world'), ), ``` Prior art: https://github.com/flutter/flutter/pull/165597 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter/lib/src/painting/alignment.dart | 67 ++++++++++++++++++- .../flutter/test/painting/alignment_test.dart | 12 ++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/alignment.dart b/packages/flutter/lib/src/painting/alignment.dart index 6d77171bcb0..6f3903406e6 100644 --- a/packages/flutter/lib/src/painting/alignment.dart +++ b/packages/flutter/lib/src/painting/alignment.dart @@ -15,8 +15,8 @@ import 'basic_types.dart'; /// Base class for [Alignment] that allows for text-direction aware /// resolution. /// -/// A property or argument of this type accepts classes created either with [ -/// Alignment] and its variants, or [AlignmentDirectional.new]. +/// A property or argument of this type accepts classes created either with +/// [Alignment] and its variants, or [AlignmentDirectional.new]. /// /// To convert an [AlignmentGeometry] object of indeterminate type into an /// [Alignment] object, call the [resolve] method. @@ -32,6 +32,69 @@ abstract class AlignmentGeometry { /// Creates a directional alignment, or [AlignmentDirectional]. const factory AlignmentGeometry.directional(double start, double y) = AlignmentDirectional; + /// The top left corner. + /// + /// See also: + /// + /// * [Alignment.topLeft], which is the same thing. + static const AlignmentGeometry topLeft = Alignment.topLeft; + + /// The center point along the top edge. + /// + /// See also: + /// + /// * [Alignment.topCenter], which is the same thing. + static const AlignmentGeometry topCenter = Alignment.topCenter; + + /// The top right corner. + /// + /// See also: + /// + /// * [Alignment.topRight], which is the same thing. + static const AlignmentGeometry topRight = Alignment.topRight; + + /// The center point along the left edge. + /// + /// See also: + /// + /// * [Alignment.centerLeft], which is the same thing. + static const AlignmentGeometry centerLeft = Alignment.centerLeft; + + /// The center point, both horizontally and vertically. + /// + /// See also: + /// + /// * [Alignment.center], which is the same thing. + static const AlignmentGeometry center = Alignment.center; + + /// The center point along the right edge. + /// + /// See also: + /// + /// * [Alignment.centerRight], which is the same thing. + static const AlignmentGeometry centerRight = Alignment.centerRight; + + /// The bottom left corner. + /// + /// See also: + /// + /// * [Alignment.bottomLeft], which is the same thing. + static const AlignmentGeometry bottomLeft = Alignment.bottomLeft; + + /// The center point along the bottom edge. + /// + /// See also: + /// + /// * [Alignment.bottomCenter], which is the same thing. + static const AlignmentGeometry bottomCenter = Alignment.bottomCenter; + + /// The bottom right corner. + /// + /// See also: + /// + /// * [Alignment.bottomRight], which is the same thing. + static const AlignmentGeometry bottomRight = Alignment.bottomRight; + double get _x; double get _start; diff --git a/packages/flutter/test/painting/alignment_test.dart b/packages/flutter/test/painting/alignment_test.dart index fa6612087d8..815723b1934 100644 --- a/packages/flutter/test/painting/alignment_test.dart +++ b/packages/flutter/test/painting/alignment_test.dart @@ -338,4 +338,16 @@ void main() { expect(const AlignmentGeometry.xy(4, 5), const Alignment(4, 5)); expect(const AlignmentGeometry.directional(4, 5), const AlignmentDirectional(4, 5)); }); + + test('AlignmentGeometry static members', () { + expect(AlignmentGeometry.topLeft, Alignment.topLeft); + expect(AlignmentGeometry.topCenter, Alignment.topCenter); + expect(AlignmentGeometry.topRight, Alignment.topRight); + expect(AlignmentGeometry.centerLeft, Alignment.centerLeft); + expect(AlignmentGeometry.center, Alignment.center); + expect(AlignmentGeometry.centerRight, Alignment.centerRight); + expect(AlignmentGeometry.bottomLeft, Alignment.bottomLeft); + expect(AlignmentGeometry.bottomCenter, Alignment.bottomCenter); + expect(AlignmentGeometry.bottomRight, Alignment.bottomRight); + }); }