When the app enters the detached state, clear the record of the last SystemChrome style sent to the host (#153586)

This ensures that the style will be sent again after the host is reattached

Fixes https://github.com/flutter/flutter/issues/150418
This commit is contained in:
Jason Simmons
2024-08-20 10:33:13 -07:00
committed by GitHub
parent 4a03b76c68
commit cdab9d509b
3 changed files with 44 additions and 1 deletions

View File

@@ -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<String?> _handleLifecycleMessage(String? message) async {
final AppLifecycleState? state = _parseAppLifecycleMessage(message!);
final List<AppLifecycleState> generated = _generateStateTransitions(lifecycleState, state!);
generated.forEach(handleAppLifecycleStateChanged);
for (final AppLifecycleState stateChange in generated) {
handleAppLifecycleStateChanged(stateChange);
SystemChrome.handleAppLifecycleStateChanged(stateChange);
}
return null;
}

View File

@@ -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].

View File

@@ -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<MethodCall> log = <MethodCall>[];
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));
});
}