Skip to content

Commit 66cb2dc

Browse files
committed
docs: Add dot shorthands guide to Flutter concepts
1 parent a9d3279 commit 66cb2dc

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

src/content/ui/dot-shorthands.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "Dot shorthands in Flutter"
3+
description: "Learn how to use Dart's dot shorthands to write cleaner, concise Flutter code."
4+
---
5+
6+
The **dot shorthands** feature allows you to omit the explicit type when
7+
accessing static members, constructors, or enum values, provided the compiler
8+
can infer the type from the surrounding context.
9+
10+
:::note
11+
For n technical overview of this feature, refer to the
12+
[Dot Shorthands guide](https://dart.dev/language/dot-shorthand) in the Dart
13+
documentation.
14+
:::
15+
16+
## Why dot shorthands matter
17+
18+
Building layouts in Flutter often involves deeply nested widget trees.
19+
Historically, this meant repeatedly typing explicit class and enum names for
20+
properties like colors, typography, and alignment. Dot shorthands reduce this
21+
boilerplate, making your code easier to read and faster to write.
22+
23+
Here is a side-by-side comparison of building a simple `Container`:
24+
25+
### Without dot shorthands
26+
```dart
27+
Container(
28+
color: Colors.blue,
29+
alignment: Alignment.center,
30+
padding: const EdgeInsets.all(16.0),
31+
child: Column(
32+
mainAxisAlignment: MainAxisAlignment.center,
33+
crossAxisAlignment: CrossAxisAlignment.start,
34+
children: [
35+
Text(
36+
'Hello World',
37+
style: TextStyle(
38+
fontWeight: FontWeight.bold,
39+
),
40+
),
41+
],
42+
),
43+
);
44+
```
45+
46+
### With dot shorthands
47+
```dart
48+
Container(
49+
color: .blue, // Instead of Colors.blue
50+
alignment: .center, // Instead of Alignment.center
51+
padding: const .all(16.0), // Instead of EdgeInsets.all(16.0)
52+
child: Column(
53+
mainAxisAlignment: .center, // Instead of MainAxisAlignment.center
54+
crossAxisAlignment: .start, // Instead of CrossAxisAlignment.start
55+
children: [
56+
Text(
57+
'Hello World',
58+
style: TextStyle(
59+
fontWeight: .bold, // Instead of FontWeight.bold
60+
),
61+
),
62+
],
63+
),
64+
);
65+
```
66+
67+
## Where to use dot shorthands
68+
69+
Dot shorthands work anywhere the Dart compiler has a clear "context type",
70+
meaning it knows exactly what type it expects. In Flutter, this is almost
71+
everywhere inside a widget's property list.
72+
73+
The most common targets for dot shorthands in Flutter are:
74+
75+
* **Enums**: `MainAxisAlignment`, `CrossAxisAlignment`, `BoxFit`, `TextDirection`.
76+
* **Static properties and methods**: `Colors`, `Icons`, `Alignment`.
77+
* **Constructors**: `EdgeInsets.all()`, `BorderRadius.circular()`.
78+
79+
### Example: enums
80+
81+
When a property expects an enum, such as `mainAxisAlignment`, you can omit the
82+
enum's name and just provide the value preceded by a dot (`.`):
83+
84+
```dart
85+
Row(
86+
mainAxisAlignment: .spaceEvenly, // Infers MainAxisAlignment.spaceEvenly
87+
children: [ /* ... */ ],
88+
)
89+
```
90+
91+
### Example: static properties
92+
93+
`Colors.green` and `Alignment.bottomRight` are static properties on their
94+
respective classes. Since `color` expects a `Color` object, you can just write
95+
`.green`:
96+
97+
```dart
98+
DecoratedBox(
99+
decoration: BoxDecoration(
100+
color: .green, // Infers Colors.green
101+
),
102+
child: Align(
103+
alignment: .bottomRight, // Infers Alignment.bottomRight
104+
child: FlutterLogo(),
105+
),
106+
)
107+
```
108+
109+
### Example: constructors
110+
111+
You can also use dot shorthands for named constructors. A common case is
112+
`EdgeInsets`:
113+
114+
```dart
115+
Padding(
116+
padding: .symmetric(horizontal: 16.0, vertical: 8.0), // Infers EdgeInsets.symmetric
117+
child: Text('Spaced out text'),
118+
)
119+
```
120+
121+
You can even use `.new` to call an unnamed constructor, though this is less
122+
common in standard widget trees:
123+
124+
```dart
125+
class _MyState extends State<MyWidget> {
126+
final ScrollController _scrollController = .new(); // Infers ScrollController()
127+
// ...
128+
}
129+
```

src/data/sidenav/default.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@
753753
permalink: /testing/build-modes
754754
- title: Hot reload
755755
permalink: /tools/hot-reload
756+
- title: Dot shorthands
757+
permalink: /ui/dot-shorthands
756758

757759
- title: App solutions
758760
icon: apps

0 commit comments

Comments
 (0)