diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index 471e3a2e9b..b1b084074d 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -27,6 +27,7 @@ import 'raw_keyboard.dart' show RawKeyboard; import 'restoration.dart'; import 'service_extensions.dart'; import 'system_channels.dart'; +import 'system_chrome.dart'; import 'text_input.dart'; export 'dart:ui' show ChannelBuffers, RootIsolateToken; @@ -284,7 +285,10 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { Future _handleLifecycleMessage(String? message) async { final AppLifecycleState? state = _parseAppLifecycleMessage(message!); final List generated = _generateStateTransitions(lifecycleState, state!); - generated.forEach(handleAppLifecycleStateChanged); + for (final AppLifecycleState stateChange in generated) { + handleAppLifecycleStateChanged(stateChange); + SystemChrome.handleAppLifecycleStateChanged(stateChange); + } return null; } diff --git a/packages/flutter/lib/src/services/system_chrome.dart b/packages/flutter/lib/src/services/system_chrome.dart index 9d2a2608f9..515216f8a0 100644 --- a/packages/flutter/lib/src/services/system_chrome.dart +++ b/packages/flutter/lib/src/services/system_chrome.dart @@ -641,6 +641,17 @@ abstract final class SystemChrome { }); } + /// Called by the binding during a transition to a new app lifecycle state. + static void handleAppLifecycleStateChanged(AppLifecycleState state) { + // When the app is detached, clear the record of the style sent to the host + // so that it will be sent again when the app is reattached. + if (state == AppLifecycleState.detached) { + scheduleMicrotask(() { + _latestStyle = null; + }); + } + } + static SystemUiOverlayStyle? _pendingStyle; /// The last style that was set using [SystemChrome.setSystemUIOverlayStyle]. diff --git a/packages/flutter/test/services/system_chrome_test.dart b/packages/flutter/test/services/system_chrome_test.dart index 555d9e2197..b427f889d2 100644 --- a/packages/flutter/test/services/system_chrome_test.dart +++ b/packages/flutter/test/services/system_chrome_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui' as ui show AppLifecycleState; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -200,4 +201,31 @@ void main() { 'systemNavigationBarContrastEnforced: null})', ); }); + + testWidgets('SystemChrome handles detached lifecycle state', (WidgetTester tester) async { + final List log = []; + + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (MethodCall methodCall) async { + log.add(methodCall); + return null; + }); + + const SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(); + SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle); + await tester.idle(); + expect(log.length, equals(1)); + + // Setting the same state should not send another message to the host. + SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle); + await tester.idle(); + expect(log.length, equals(1)); + + // The state should be sent again if the app was detached. + SystemChrome.handleAppLifecycleStateChanged(ui.AppLifecycleState.detached); + await tester.idle(); + SystemChrome.handleAppLifecycleStateChanged(ui.AppLifecycleState.resumed); + SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle); + await tester.idle(); + expect(log.length, equals(2)); + }); }