[flutter_tool][gallery] Standardize target platform override behavior in tool and update flutter gallery to support it (#46206)

diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
index 949c1a4..218bc0c 100644
--- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
+++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
@@ -235,23 +235,12 @@
       final vmservice.Response response = await _vmService
           ?.callServiceExtension('ext.flutter.platformOverride');
       final String currentPlatform = response.json['value'] as String;
-      String nextPlatform;
-      switch (currentPlatform) {
-        case 'android':
-          nextPlatform = 'iOS';
-          break;
-        case 'iOS':
-          nextPlatform = 'android';
-          break;
-      }
-      if (nextPlatform == null) {
-        return;
-      }
+      final String platform = nextPlatform(currentPlatform, featureFlags);
       await _vmService?.callServiceExtension('ext.flutter.platformOverride',
           args: <String, Object>{
-            'value': nextPlatform,
+            'value': platform,
           });
-      printStatus('Switched operating system to $nextPlatform');
+      printStatus('Switched operating system to $platform');
     } on vmservice.RPCError {
       return;
     }
diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart
index e666af6..aaadc55 100644
--- a/packages/flutter_tools/lib/src/resident_runner.dart
+++ b/packages/flutter_tools/lib/src/resident_runner.dart
@@ -363,16 +363,7 @@
   }
 
   Future<String> togglePlatform({ String from }) async {
-    String to;
-    switch (from) {
-      case 'iOS':
-        to = 'android';
-        break;
-      case 'android':
-      default:
-        to = 'iOS';
-        break;
-    }
+    final String to = nextPlatform(from, featureFlags);
     for (FlutterView view in views) {
       await view.uiIsolate.flutterPlatformOverride(to);
     }
@@ -1319,3 +1310,26 @@
   final Uri wsUri;
   final String baseUri;
 }
+
+/// Returns the next platform value for the switcher.
+///
+/// These values must match what is available in
+/// packages/flutter/lib/src/foundation/binding.dart
+String nextPlatform(String currentPlatform, FeatureFlags featureFlags) {
+  switch (currentPlatform) {
+    case 'android':
+      return 'iOS';
+    case 'iOS':
+      return 'fuchsia';
+    case 'fuchsia':
+      if (featureFlags.isMacOSEnabled) {
+        return 'macOS';
+      }
+      return 'android';
+    case 'macOS':
+      return 'android';
+    default:
+      assert(false); // Invalid current platform.
+      return 'android';
+  }
+}
diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart
index 0dd418d..187991f 100644
--- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart
@@ -704,6 +704,14 @@
       Device device,
     }) async => mockVMService,
   }));
+
+  test('nextPlatform moves through expected platforms', () {
+    expect(nextPlatform('android', TestFeatureFlags()), 'iOS');
+    expect(nextPlatform('iOS', TestFeatureFlags()), 'fuchsia');
+    expect(nextPlatform('fuchsia', TestFeatureFlags()), 'android');
+    expect(nextPlatform('fuchsia', TestFeatureFlags(isMacOSEnabled: true)), 'macOS');
+    expect(() => nextPlatform('unknown', TestFeatureFlags()), throwsA(isInstanceOf<AssertionError>()));
+  });
 }
 
 class MockFlutterDevice extends Mock implements FlutterDevice {}
diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
index a747a83..405e438 100644
--- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
@@ -773,9 +773,9 @@
 
     await residentWebRunner.debugTogglePlatform();
 
-    expect(testLogger.statusText, contains('Switched operating system to android'));
+    expect(testLogger.statusText, contains('Switched operating system to fuchsia'));
     verify(mockVmService.callServiceExtension('ext.flutter.platformOverride',
-        args: <String, Object>{'value': 'android'})).called(1);
+        args: <String, Object>{'value': 'fuchsia'})).called(1);
   }));
 
   test('cleanup of resources is safe to call multiple times', () => testbed.run(() async {