Skip to content

Commit

Permalink
When the app enters the detached state, clear the record of the last …
Browse files Browse the repository at this point in the history
…SystemChrome style sent to the host (flutter#153586)

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

Fixes flutter#150418
  • Loading branch information
jason-simmons authored Aug 20, 2024
1 parent 4a03b76 commit cdab9d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/flutter/lib/src/services/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/flutter/lib/src/services/system_chrome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
28 changes: 28 additions & 0 deletions packages/flutter/test/services/system_chrome_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
});
}

0 comments on commit cdab9d5

Please sign in to comment.