Skip to content

Commit 3ea2d72

Browse files
document StaticIconProvider (flutter#120935)
document StaticIconProvider
1 parent ba097e2 commit 3ea2d72

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

packages/flutter/lib/src/widgets/icon_data.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import 'package:flutter/foundation.dart';
88
///
99
/// See [Icons] for a number of predefined icons available for material
1010
/// design applications.
11+
///
12+
/// In release builds, the Flutter tool will tree shake out of bundled fonts
13+
/// the code points (or instances of [IconData]) which are not referenced from
14+
/// Dart app code. See the [staticIconProvider] annotation for more details.
1115
@immutable
1216
class IconData {
1317
/// Creates icon data.
@@ -17,6 +21,11 @@ class IconData {
1721
///
1822
/// The [fontPackage] argument must be non-null when using a font family that
1923
/// is included in a package. This is used when selecting the font.
24+
///
25+
/// Instantiating non-const instances of this class in your app will
26+
/// mean the app cannot be built in release mode with icon tree-shaking (it
27+
/// need to be explicitly opted out at build time). See [staticIconProvider]
28+
/// for more context.
2029
const IconData(
2130
this.codePoint, {
2231
this.fontFamily,
@@ -99,6 +108,20 @@ class _StaticIconProvider {
99108
/// Annotation for classes that only provide static const [IconData] instances.
100109
///
101110
/// This is a hint to the font tree shaker to ignore the constant instances
102-
/// of [IconData] appearing in the class when tracking which code points
103-
/// should be retained in the bundled font.
111+
/// of [IconData] appearing in the declaration of this class when tree-shaking
112+
/// unused code points from the bundled font.
113+
///
114+
/// Classes with this annotation must have only "static const" members. The
115+
/// presence of any non-const [IconData] instances will preclude apps
116+
/// importing the declaration into their application from being able to use
117+
/// icon tree-shaking during release builds, resulting in larger font assets.
118+
///
119+
/// ```dart
120+
/// @staticIconProvider
121+
/// abstract final class MyCustomIcons {
122+
/// static const String fontFamily = 'MyCustomIcons';
123+
/// static const IconData happyFace = IconData(1, fontFamily: fontFamily);
124+
/// static const IconData sadFace = IconData(2, fontFamily: fontFamily);
125+
/// }
126+
/// ```
104127
const Object staticIconProvider = _StaticIconProvider();

0 commit comments

Comments
 (0)