Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/content/ui/dot-shorthands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
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 n technical overview of this feature, refer to the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For n technical overview of this feature, refer to the
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 reduce this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
properties like colors, typography, and alignment. Dot shorthands reduce this
properties like colors, typography, and alignment. Dot shorthands reduces this

"Dot shorthands" might end with an "s", but it is a singular noun.

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(
color: Colors.blue,
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(
color: .blue, // Instead of Colors.blue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, Colors is currently incompatible with dot shorthands. APIs like Container.color accept a Color, so Dart cannot infer the Colors type.

This problem also affects:

  1. Icons for APIs that accept IconData, like IconButton.
  2. Curves for APIs that accept a Curve, like CurvedAnimation.

We considered changing the Color API to make it compatible with dot shorthands. See: flutter.dev/go/basic-color-set

In the future, we might fix this using Dart's upcoming static extensions feature, which will let the Material library "inject" colors onto the Color type, like Color.blue. However, there's no ETA on this.

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**: `Colors`, `Icons`, `Alignment`.
Copy link
Member

@loic-sharma loic-sharma Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, both Colors and Icons are currently incompatible with dot shorthands. I'd remove these from this category. Off-hand, I can't remember of another common API that's in this bucket.

* **Constructors**: `EdgeInsets.all()`, `BorderRadius.circular()`.

### Example: enums

When a property expects an enum, such as `mainAxisAlignment`, you can omit the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When a property expects an enum, such as `mainAxisAlignment`, you can omit the
When a property expects an `enum`, such as `mainAxisAlignment`, you can omit the

enum's name and just provide the value preceded by a dot (`.`):

```dart
Row(
mainAxisAlignment: .spaceEvenly, // Infers MainAxisAlignment.spaceEvenly
children: [ /* ... */ ],
)
```

### Example: static properties

`Colors.green` and `Alignment.bottomRight` are static properties on their
respective classes. Since `color` expects a `Color` object, you can just write
`.green`:

```dart
DecoratedBox(
decoration: BoxDecoration(
color: .green, // Infers Colors.green
),
child: Align(
alignment: .bottomRight, // Infers Alignment.bottomRight
child: FlutterLogo(),
),
)
```

### Example: constructors

You can also use dot shorthands for named constructors. A common case is
`EdgeInsets`:

```dart
Padding(
padding: .symmetric(horizontal: 16.0, vertical: 8.0), // Infers EdgeInsets.symmetric
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually infers to EdgeInsetsGeometry.symmetric.


Paddding.padding accepts a EdgeInsetsGeometry, which is the common base type between EdgeInsets and EdgeInsetsDirectional. We added a bunch of redirecting constructors to EdgeInsetsGeometry to make it "compatible" with dot shorthands.

For example, see EdgeInsetsGeometry.symmetric which redirects to EdgeInsets.symmetric: source code.

child: Text('Spaced out text'),
)
```

You can even use `.new` to call an unnamed constructor, though this is less
common in standard widget trees:

```dart
class _MyState extends State<MyWidget> {
final ScrollController _scrollController = .new(); // Infers ScrollController()
// ...
}
```
2 changes: 2 additions & 0 deletions src/data/sidenav/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@
permalink: /testing/build-modes
- title: Hot reload
permalink: /tools/hot-reload
- title: Dot shorthands
permalink: /ui/dot-shorthands

- title: App solutions
icon: apps
Expand Down
Loading