diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d3a6d06..d3b9f6f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## [0.0.6] * Update `Typography` with correct letter spacing and font weights +* Add `brightnessOf` and `maybeBrightnessOf` functions to `MacosTheme` ## [0.0.5] * Adds the `PushButton` widget along with `PushButtonTheme` and `PushButtonThemeData` diff --git a/lib/src/styles/macos_theme.dart b/lib/src/styles/macos_theme.dart index 3d641aa8..b8d484a0 100644 --- a/lib/src/styles/macos_theme.dart +++ b/lib/src/styles/macos_theme.dart @@ -35,6 +35,12 @@ class MacosTheme extends StatelessWidget { /// {@macro flutter.widgets.ProxyWidget.child} final Widget child; + /// Retrieves the [MacosThemeData] from the closest ancestor [MacosTheme] + /// widget, or a default [MacosThemeData] if no [MacosTheme] ancestor + /// exists. + /// + /// Resolves all the colors defined in that [MacosThemeData] against the + /// given [BuildContext] on a best-effort basis. static MacosThemeData of(BuildContext context) { final _InheritedMacosTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedMacosTheme>(); @@ -42,6 +48,50 @@ class MacosTheme extends StatelessWidget { .resolveFrom(context); } + /// Retrieves the [Brightness] to use for descendant macOS widgets, based + /// on the value of [MacosThemeData.brightness] in the given [context]. + /// + /// If no [MacosTheme] can be found in the given [context], or its `brightness` + /// is null, it will fall back to [MediaQueryData.platformBrightness]. + /// + /// Throws an exception if no valid [MacosTheme] or [MediaQuery] widgets + /// exist in the ancestry tree. + /// + /// See also: + /// + /// * [maybeBrightnessOf], which returns null if no valid [MacosTheme] or + /// [MediaQuery] exists, instead of throwing. + /// * [MacosThemeData.brightness], the property takes precedence over + /// [MediaQueryData.platformBrightness] for descendant Cupertino widgets. + static Brightness brightnessOf(BuildContext context) { + final _InheritedMacosTheme? inheritedTheme = + context.dependOnInheritedWidgetOfExactType<_InheritedMacosTheme>(); + return inheritedTheme?.theme.data.brightness ?? + MediaQuery.of(context).platformBrightness; + } + + /// Retrieves the [Brightness] to use for descendant macOS widgets, based + /// on the value of [MacosThemeData.brightness] in the given [context]. + /// + /// If no [MacosTheme] can be found in the given [context], it will fall + /// back to [MediaQueryData.platformBrightness]. + /// + /// Returns null if no valid [MacosTheme] or [MediaQuery] widgets exist in + /// the ancestry tree. + /// + /// See also: + /// + /// * [MacosThemeData.brightness], the property takes precedence over + /// [MediaQueryData.platformBrightness] for descendant macOS widgets. + /// * [brightnessOf], which throws if no valid [MacosTheme] or + /// [MediaQuery] exists, instead of returning null. + static Brightness? maybeBrightnessOf(BuildContext context) { + final _InheritedMacosTheme? inheritedTheme = + context.dependOnInheritedWidgetOfExactType<_InheritedMacosTheme>(); + return inheritedTheme?.theme.data.brightness ?? + MediaQuery.maybeOf(context)?.platformBrightness; + } + @override Widget build(BuildContext context) { return _InheritedMacosTheme(