|
| 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 a 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 reduces 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 | + alignment: Alignment.center, |
| 29 | + padding: const EdgeInsets.all(16.0), |
| 30 | + child: Column( |
| 31 | + mainAxisAlignment: MainAxisAlignment.center, |
| 32 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 33 | + children: [ |
| 34 | + Text( |
| 35 | + 'Hello World', |
| 36 | + style: TextStyle( |
| 37 | + fontWeight: FontWeight.bold, |
| 38 | + ), |
| 39 | + ), |
| 40 | + ], |
| 41 | + ), |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +### With dot shorthands |
| 46 | +```dart |
| 47 | +Container( |
| 48 | + alignment: .center, // Instead of Alignment.center, |
| 49 | + padding: const .all(16.0), // Instead of EdgeInsets.all(16.0) |
| 50 | + child: Column( |
| 51 | + mainAxisAlignment: .center, // Instead of MainAxisAlignment.center |
| 52 | + crossAxisAlignment: .start, // Instead of CrossAxisAlignment.start |
| 53 | + children: [ |
| 54 | + Text( |
| 55 | + 'Hello World', |
| 56 | + style: TextStyle( |
| 57 | + fontWeight: .bold, // Instead of FontWeight.bold |
| 58 | + ), |
| 59 | + ), |
| 60 | + ], |
| 61 | + ), |
| 62 | +); |
| 63 | +``` |
| 64 | + |
| 65 | +## Where to use dot shorthands |
| 66 | + |
| 67 | +Dot shorthands work anywhere the Dart compiler has a clear "context type", |
| 68 | +meaning it knows exactly what type it expects. In Flutter, this is almost |
| 69 | +everywhere inside a widget's property list. |
| 70 | + |
| 71 | +The most common targets for dot shorthands in Flutter are: |
| 72 | + |
| 73 | +* **Enums**: `MainAxisAlignment`, `CrossAxisAlignment`, `BoxFit`, `TextDirection`. |
| 74 | +* **Static properties and methods**: `FontWeight` (constants like `.bold`). |
| 75 | +* **Constructors**: `EdgeInsets.all()`, `BorderRadius.circular()`. |
| 76 | + |
| 77 | +### Example: enums |
| 78 | + |
| 79 | +When a property expects an `enum`, such as `mainAxisAlignment`, you can omit the |
| 80 | +enum's name and just provide the value preceded by a dot (`.`): |
| 81 | + |
| 82 | +```dart |
| 83 | +Row( |
| 84 | + mainAxisAlignment: .spaceEvenly, // Infers MainAxisAlignment.spaceEvenly |
| 85 | + children: [ /* ... */ ], |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +### Example: static properties |
| 90 | + |
| 91 | +Static properties work when the context type is exactly the class that defines the property. A common example is text styling with `FontWeight`: |
| 92 | + |
| 93 | +```dart |
| 94 | +Text( |
| 95 | + 'Feature highlights', |
| 96 | + style: TextStyle( |
| 97 | + fontWeight: .bold, // Infers FontWeight.bold |
| 98 | + ), |
| 99 | +) |
| 100 | +``` |
| 101 | + |
| 102 | +### Example: constructors |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +```dart |
| 107 | +Padding( |
| 108 | + padding: .symmetric(horizontal: 16.0, vertical: 8.0), // Infers EdgeInsetsGeometry.symmetric |
| 109 | + child: Text('Spaced out text'), |
| 110 | +) |
| 111 | +``` |
| 112 | + |
| 113 | +You can even use `.new` to call an unnamed constructor, though this is less |
| 114 | +common in standard widget trees: |
| 115 | + |
| 116 | +```dart |
| 117 | +class _MyState extends State<MyWidget> { |
| 118 | + final ScrollController _scrollController = .new(); // Infers ScrollController() |
| 119 | + // ... |
| 120 | +} |
| 121 | +``` |
0 commit comments