You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add static of accessor methods to ColorScheme and TextTheme (#154073)
The most common use case to lookup a `ThemeData` instance using `Theme.of(context)` is to access either the `ColorScheme`, the `TextTheme`, or both.
Before this change:
```dart
final colors = Theme.of(context).colorScheme;
final textTheme = Theme.of(context).textTheme;
final primaryTextTheme = Theme.of(context).primaryTextTheme;
```
or
```dart
final ThemeData(
:colorScheme,
:textTheme,
:primaryTextTheme,
) = Theme.of(context);
```
After this change:
```dart
final colors = ColorScheme.of(context);
final textTheme = TextTheme.of(context);
final primaryTextTheme = TextTheme.primaryOf(context);
```
### Primary Changes
This PR adds static `of` convenience methods to `ColorScheme` and `TextTheme` that delegate to the `ThemeData`'s respective properties. The methods added are:
* `ColorScheme.of(context)` that returns `Theme.of(context).colorScheme`.
* `TextTheme.of(context)` that returns `Theme.of(context).textTheme`.
* `TextTheme.primaryOf(context)` that returns `Theme.of(context).primaryTextTheme`.
### Side-effects
To allow the above changes to function, this PR adds:
* A `theme.dart` import to `color_scheme.dart` to access to `Theme`.
* A `theme.dart` import to `text_theme.dart` to access to `Theme`.
* A `package:flutter/widgets.dart` import to `text_theme.dart` to access `BuildContext`.
* The above import allowed getting rid of the same `@docImport` from `text_theme.dart`.
### Documentation updates
This PR also updates the following documentation elements:
* Adds docs to the newly added members.
* Updates `TextTheme`'s docs to instruct using `TextTheme.of(context)` instead of `Theme.of(context).textTheme`.
* Updates `Theme.of` to add a "See also" section to `ColorScheme.of` and `TextTheme.of` since these use cases are among the most common ones for `Theme.of(context)`.
Fixes #72201.
0 commit comments