-
Notifications
You must be signed in to change notification settings - Fork 3.4k
docs: Add dot shorthands guide to Flutter concepts #13180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+123
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66cb2dc
docs: Add dot shorthands guide to Flutter concepts
lamek 65f95ac
Undo extra cookbook changes
lamek ba0010f
Update dot shorthands guide
lamek 591e45a
Merge branch 'main' of https://github.com/flutter/website into clean-…
lamek 7671792
Merge branch 'main' into clean-dot-shorthands
lamek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| --- | ||
| title: "Dot shorthands in Flutter" | ||
| description: "Learn how to use Dart's dot shorthands to write cleaner, concise Flutter code." | ||
| --- | ||
|
|
||
| The **dot shorthands** feature allows you to omit the explicit type when | ||
| accessing static members, constructors, or enum values, provided the compiler | ||
| can infer the type from the surrounding context. | ||
|
|
||
| :::note | ||
| For a technical overview of this feature, refer to the | ||
| [Dot Shorthands guide](https://dart.dev/language/dot-shorthand) in the Dart | ||
| documentation. | ||
| ::: | ||
|
|
||
| ## Why dot shorthands matter | ||
|
|
||
| Building layouts in Flutter often involves deeply nested widget trees. | ||
| Historically, this meant repeatedly typing explicit class and enum names for | ||
| properties like colors, typography, and alignment. Dot shorthands reduces this | ||
| boilerplate, making your code easier to read and faster to write. | ||
|
|
||
| Here is a side-by-side comparison of building a simple `Container`: | ||
|
|
||
| ### Without dot shorthands | ||
| ```dart | ||
| Container( | ||
| alignment: Alignment.center, | ||
| padding: const EdgeInsets.all(16.0), | ||
| child: Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| Text( | ||
| 'Hello World', | ||
| style: TextStyle( | ||
| fontWeight: FontWeight.bold, | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| ``` | ||
|
|
||
| ### With dot shorthands | ||
| ```dart | ||
| Container( | ||
| alignment: .center, // Instead of Alignment.center, | ||
| padding: const .all(16.0), // Instead of EdgeInsets.all(16.0) | ||
| child: Column( | ||
| mainAxisAlignment: .center, // Instead of MainAxisAlignment.center | ||
| crossAxisAlignment: .start, // Instead of CrossAxisAlignment.start | ||
| children: [ | ||
| Text( | ||
| 'Hello World', | ||
| style: TextStyle( | ||
| fontWeight: .bold, // Instead of FontWeight.bold | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| ``` | ||
|
|
||
| ## Where to use dot shorthands | ||
|
|
||
| Dot shorthands work anywhere the Dart compiler has a clear "context type", | ||
| meaning it knows exactly what type it expects. In Flutter, this is almost | ||
| everywhere inside a widget's property list. | ||
|
|
||
| The most common targets for dot shorthands in Flutter are: | ||
|
|
||
| * **Enums**: `MainAxisAlignment`, `CrossAxisAlignment`, `BoxFit`, `TextDirection`. | ||
| * **Static properties and methods**: `FontWeight` (constants like `.bold`). | ||
| * **Constructors**: `EdgeInsets.all()`, `BorderRadius.circular()`. | ||
|
|
||
| ### Example: enums | ||
|
|
||
| When a property expects an `enum`, such as `mainAxisAlignment`, you can omit the | ||
|
lamek marked this conversation as resolved.
|
||
| enum's name and just provide the value preceded by a dot (`.`): | ||
|
|
||
| ```dart | ||
| Row( | ||
| mainAxisAlignment: .spaceEvenly, // Infers MainAxisAlignment.spaceEvenly | ||
| children: [ /* ... */ ], | ||
| ) | ||
| ``` | ||
|
|
||
| ### Example: static properties | ||
|
|
||
| Static properties work when the context type is exactly the class that defines the property. A common example is text styling with `FontWeight`: | ||
|
|
||
| ```dart | ||
| Text( | ||
| 'Feature highlights', | ||
| style: TextStyle( | ||
| fontWeight: .bold, // Infers FontWeight.bold | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
| ### Example: constructors | ||
|
|
||
| You can also use dot shorthands for named constructors. Many Flutter layout properties accept a base class like `EdgeInsetsGeometry`. To support dot shorthands, Flutter adds redirecting constructors to these base classes that point to the appropriate subclasses. | ||
|
|
||
| ```dart | ||
| Padding( | ||
| padding: .symmetric(horizontal: 16.0, vertical: 8.0), // Infers EdgeInsetsGeometry.symmetric | ||
|
lamek marked this conversation as resolved.
|
||
| child: Text('Spaced out text'), | ||
| ) | ||
| ``` | ||
|
|
||
| You can even use `.new` to call an unnamed constructor, though this is less | ||
| common in standard widget trees: | ||
|
lamek marked this conversation as resolved.
|
||
|
|
||
| ```dart | ||
| class _MyState extends State<MyWidget> { | ||
| final ScrollController _scrollController = .new(); // Infers ScrollController() | ||
| // ... | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.