flutter / flutter/flutter

debugBrightnessOverride doesn't change brightness in runtime

Open
#142,412 2 comments 0 reactions 0 assignees View on GitHub
found in release: 3.16 found in release: 3.19 framework has reproducible steps P3 team-framework triaged-framework
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

`debugBrightnessOverride` doesn't change brightness when app is running. `debugBrightnessOverride` is a global variable and changing its value doesn't rebuild `MediaQuery`, `Theme` etc. There's a reason why I'm using this variable - I'm working on some tools that work in debug mode only.

### Steps to reproduce

1. Add a button that flips the value of `debugBrightnessOverride` from light to dark or from dark to light
2. Tap the button
3. `MediaQuery` and `Theme` objects still use the old value until next hot reload

### Expected results

Changing `debugBrightnessOverride` notifies framework about new value, that should trigger a rebuild of `MediaQuery` and all widgets that depend on platform brightness.

Alternatively, there could be a way to let framework know that value of `debugBrightnessOverride` has been changed and rebuild could be triggered manually (see code sample, calling various methods of `PlatformDispatcher` doesn't trigger updates)

### Actual results

Value of `debugBrightnessOverride` changes correctly but `MediaQuery.platformBrightnessOf(context)` and `Theme.of(context).brightness` still have old values, `MaterialApp` uses theme based on wrong value of platform brightness

### Code sample

Code sample

```dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State createState() => _MyAppState();
}

class _MyAppState extends State {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(),
darkTheme: ThemeData.dark(),
home: Scaffold(
body: Builder(
builder: (context) {
final platformBrightness = MediaQuery.platformBrightnessOf(context);
final themeBrightness = Theme.of(context).brightness;

return ListView(
padding: const EdgeInsets.all(24),
children: [
Text(
'debugBrightnessOverride: $debugBrightnessOverride',
),
Text(
'platformBrightness: $platformBrightness',
),
Text(
'themeBrightness: $themeBrightness',
),
ElevatedButton(
onPressed: () {
debugBrightnessOverride = switch (debugBrightnessOverride) {
Brightness.dark => Brightness.light,
Brightness.light => Brightness.dark,
null => Brightness.light,
};
PlatformDispatcher.instance.onPlatformBrightnessChanged
?.call();
PlatformDispatcher.instance.onPlatformConfigurationChanged
?.call();
setState(() {});
},
child: const Text('Flip debugBrightnessOverride'),
),
],
);
},
),
),
);
}
}

```

### Flutter Doctor output

Doctor output

```console
[✓] Flutter (Channel stable, 3.16.9, on macOS 14.1.2 23B92 darwin-arm64, locale en-FI)
• Flutter version 3.16.9 on channel stable at /Users/alexander/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 41456452f2 (3 days ago), 2024-01-25 10:06:23 -0800
• Engine revision f40e976bed
• Dart version 3.2.6
• DevTools version 2.28.5

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
• Android SDK at /Users/alexander/Library/Android/sdk
• Platform android-34, build-tools 32.0.0
• ANDROID_HOME = /Users/alexander/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.14.3

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] IntelliJ IDEA Community Edition (version 2022.3.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 72.0.4
• Dart plugin version 223.8617.8

[✓] VS Code (version 1.85.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.80.0

[✓] Connected device (3 available)
• iPhone 15 Pro (mobile) • A4B888AD-BD94-4930-8817-17C897CCF81E • ios •
com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
• macOS (desktop) • macos • darwin-arm64 • macOS 14.1.2 23B92
darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 121.0.6167.85

[✓] Network resources
• All expected network resources are available.

• No issues found!
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.