From 3bf6754d481654d2a0978d644eb00380a7b21b59 Mon Sep 17 00:00:00 2001 From: Kishan Rathore <34465683+rkishan516@users.noreply.github.com> Date: Thu, 3 Apr 2025 06:04:53 +0530 Subject: [PATCH] Migrate to Theme.brightnessOf method (#163950) Refactor: Migrate to Theme.brightnessOf method fixes: #163837 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --- .../lib/demo/material/date_and_time_picker_demo.dart | 2 +- .../flutter_gallery/lib/demo/material/menu_demo.dart | 2 +- .../flutter_gallery/lib/gallery/app.dart | 2 +- .../flutter_gallery/lib/gallery/demo.dart | 2 +- .../flutter_gallery/lib/gallery/options.dart | 2 +- .../new_gallery/lib/studies/reply/app.dart | 2 +- .../new_gallery/lib/studies/reply/mail_card_preview.dart | 2 +- packages/flutter/lib/src/material/bottom_app_bar.dart | 2 +- packages/flutter/lib/src/material/dropdown.dart | 2 +- packages/flutter/lib/src/material/expand_icon.dart | 2 +- packages/flutter/lib/src/material/icon_button.dart | 2 +- packages/flutter/lib/src/material/input_decorator.dart | 8 ++++---- packages/flutter/lib/src/material/stepper.dart | 4 ++-- packages/flutter/lib/src/material/tabs.dart | 2 +- 14 files changed, 18 insertions(+), 18 deletions(-) diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/date_and_time_picker_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/date_and_time_picker_demo.dart index db55329c54..48c9e1af0d 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/date_and_time_picker_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/date_and_time_picker_demo.dart @@ -30,7 +30,7 @@ class _InputDropdown extends StatelessWidget { Icon( Icons.arrow_drop_down, color: - Theme.of(context).brightness == Brightness.light + Theme.brightnessOf(context) == Brightness.light ? Colors.grey.shade700 : Colors.white70, ), diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/menu_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/menu_demo.dart index 49b971f928..0f71f1206b 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/menu_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/menu_demo.dart @@ -77,7 +77,7 @@ class MenuDemoState extends State { ), body: ListTileTheme( iconColor: - Theme.of(context).brightness == Brightness.light ? Colors.grey[600] : Colors.grey[500], + Theme.brightnessOf(context) == Brightness.light ? Colors.grey[600] : Colors.grey[500], child: ListView( padding: kMaterialListPadding, children: [ diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/app.dart b/dev/integration_tests/flutter_gallery/lib/gallery/app.dart index 0b7ba43f0d..f72ec78da2 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/app.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/app.dart @@ -169,7 +169,7 @@ class _GalleryAppState extends State { Builder( builder: (BuildContext context) { return CupertinoTheme( - data: CupertinoThemeData(brightness: Theme.of(context).brightness), + data: CupertinoThemeData(brightness: Theme.brightnessOf(context)), child: child!, ); }, diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart b/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart index bd6efd8b46..1d03efd6af 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart @@ -187,7 +187,7 @@ class FullScreenCodeDialogState extends State { @override Widget build(BuildContext context) { final SyntaxHighlighterStyle style = - Theme.of(context).brightness == Brightness.dark + Theme.brightnessOf(context) == Brightness.dark ? SyntaxHighlighterStyle.darkThemeStyle : SyntaxHighlighterStyle.lightThemeStyle; diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart index 9b8f98a04c..db309e646e 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart @@ -131,7 +131,7 @@ class _BooleanItem extends StatelessWidget { @override Widget build(BuildContext context) { - final bool isDark = Theme.of(context).brightness == Brightness.dark; + final bool isDark = Theme.brightnessOf(context) == Brightness.dark; return _OptionsItem( child: Row( children: [ diff --git a/dev/integration_tests/new_gallery/lib/studies/reply/app.dart b/dev/integration_tests/new_gallery/lib/studies/reply/app.dart index fa1b74c2f1..9a8c23e9cf 100644 --- a/dev/integration_tests/new_gallery/lib/studies/reply/app.dart +++ b/dev/integration_tests/new_gallery/lib/studies/reply/app.dart @@ -77,7 +77,7 @@ class _ReplyAppState extends State with RestorationMixin { final ThemeMode galleryThemeMode = GalleryOptions.of(context).themeMode; final bool isDark = galleryThemeMode == ThemeMode.system - ? Theme.of(context).brightness == Brightness.dark + ? Theme.brightnessOf(context) == Brightness.dark : galleryThemeMode == ThemeMode.dark; final ThemeData replyTheme = diff --git a/dev/integration_tests/new_gallery/lib/studies/reply/mail_card_preview.dart b/dev/integration_tests/new_gallery/lib/studies/reply/mail_card_preview.dart index fcb60e62b2..9e958b3a15 100644 --- a/dev/integration_tests/new_gallery/lib/studies/reply/mail_card_preview.dart +++ b/dev/integration_tests/new_gallery/lib/studies/reply/mail_card_preview.dart @@ -288,7 +288,7 @@ class _MailPreviewActionBar extends StatelessWidget { @override Widget build(BuildContext context) { - final bool isDark = Theme.of(context).brightness == Brightness.dark; + final bool isDark = Theme.brightnessOf(context) == Brightness.dark; final Color color = isDark ? ReplyColors.white50 : ReplyColors.blue600; final bool isDesktop = isDisplayDesktop(context); final Color starredIconColor = isStarred ? Theme.of(context).colorScheme.secondary : color; diff --git a/packages/flutter/lib/src/material/bottom_app_bar.dart b/packages/flutter/lib/src/material/bottom_app_bar.dart index 718b6c60f3..4f989ea802 100644 --- a/packages/flutter/lib/src/material/bottom_app_bar.dart +++ b/packages/flutter/lib/src/material/bottom_app_bar.dart @@ -298,7 +298,7 @@ class _BottomAppBarDefaultsM2 extends BottomAppBarTheme { @override Color? get color => - Theme.of(context).brightness == Brightness.dark ? Colors.grey[800]! : Colors.white; + Theme.brightnessOf(context) == Brightness.dark ? Colors.grey[800]! : Colors.white; @override Color? get surfaceTintColor => Theme.of(context).colorScheme.surfaceTint; diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index f7f8a0c113..9b26cb3aa0 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1465,7 +1465,7 @@ class _DropdownButtonState extends State> with WidgetsBindi Color get _iconColor { // These colors are not defined in the Material Design spec. - final Brightness brightness = Theme.of(context).brightness; + final Brightness brightness = Theme.brightnessOf(context); if (_enabled) { return widget.iconEnabledColor ?? switch (brightness) { diff --git a/packages/flutter/lib/src/material/expand_icon.dart b/packages/flutter/lib/src/material/expand_icon.dart index e860569d27..b44094b9fe 100644 --- a/packages/flutter/lib/src/material/expand_icon.dart +++ b/packages/flutter/lib/src/material/expand_icon.dart @@ -173,7 +173,7 @@ class _ExpandIconState extends State with SingleTickerProviderStateM return widget.color!; } - return switch (Theme.of(context).brightness) { + return switch (Theme.brightnessOf(context)) { Brightness.light => Colors.black54, Brightness.dark => Colors.white60, }; diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index d1db763adc..5512aae6c5 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -1002,7 +1002,7 @@ class _IconButtonM3 extends ButtonStyleButton { ButtonStyle? themeStyleOf(BuildContext context) { final IconThemeData iconTheme = IconTheme.of(context); final bool isDefaultSize = iconTheme.size == const IconThemeData.fallback().size; - final bool isDefaultColor = identical(iconTheme.color, switch (Theme.of(context).brightness) { + final bool isDefaultColor = identical(iconTheme.color, switch (Theme.brightnessOf(context)) { Brightness.light => kDefaultIconDarkColor, Brightness.dark => kDefaultIconLightColor, }); diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index bb71278988..5cf922bf83 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -5070,7 +5070,7 @@ class _InputDecoratorDefaultsM2 extends InputDecorationTheme { @override Color? get fillColor => MaterialStateColor.resolveWith((Set states) { - return switch ((Theme.of(context).brightness, states.contains(MaterialState.disabled))) { + return switch ((Theme.brightnessOf(context), states.contains(MaterialState.disabled))) { (Brightness.dark, true) => const Color(0x0DFFFFFF), // 5% white (Brightness.dark, false) => const Color(0x1AFFFFFF), // 10% white (Brightness.light, true) => const Color(0x05000000), // 2% black @@ -5086,7 +5086,7 @@ class _InputDecoratorDefaultsM2 extends InputDecorationTheme { if (states.contains(MaterialState.focused)) { return Theme.of(context).colorScheme.primary; } - return switch (Theme.of(context).brightness) { + return switch (Theme.brightnessOf(context)) { Brightness.dark => Colors.white70, Brightness.light => Colors.black45, }; @@ -5100,7 +5100,7 @@ class _InputDecoratorDefaultsM2 extends InputDecorationTheme { if (states.contains(MaterialState.focused)) { return Theme.of(context).colorScheme.primary; } - return switch (Theme.of(context).brightness) { + return switch (Theme.brightnessOf(context)) { Brightness.dark => Colors.white70, Brightness.light => Colors.black45, }; @@ -5117,7 +5117,7 @@ class _InputDecoratorDefaultsM2 extends InputDecorationTheme { if (states.contains(MaterialState.focused)) { return Theme.of(context).colorScheme.primary; } - return switch (Theme.of(context).brightness) { + return switch (Theme.brightnessOf(context)) { Brightness.dark => Colors.white70, Brightness.light => Colors.black45, }; diff --git a/packages/flutter/lib/src/material/stepper.dart b/packages/flutter/lib/src/material/stepper.dart index 17fae158b5..f353fd3e47 100644 --- a/packages/flutter/lib/src/material/stepper.dart +++ b/packages/flutter/lib/src/material/stepper.dart @@ -430,7 +430,7 @@ class _StepperState extends State with TickerProviderStateMixin { } bool _isDark() { - return Theme.of(context).brightness == Brightness.dark; + return Theme.brightnessOf(context) == Brightness.dark; } bool _isLabel() { @@ -593,7 +593,7 @@ class _StepperState extends State with TickerProviderStateMixin { ); } - final Color cancelColor = switch (Theme.of(context).brightness) { + final Color cancelColor = switch (Theme.brightnessOf(context)) { Brightness.light => Colors.black54, Brightness.dark => Colors.white70, }; diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 7df2f5dcff..084b3b0a76 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -2630,7 +2630,7 @@ class _TabsDefaultsM2 extends TabBarThemeData { final BuildContext context; late final ColorScheme _colors = Theme.of(context).colorScheme; - late final bool isDark = Theme.of(context).brightness == Brightness.dark; + late final bool isDark = Theme.brightnessOf(context) == Brightness.dark; late final Color primaryColor = isDark ? Colors.grey[900]! : Colors.blue; final bool isScrollable;