Serving a phone-friendly Flutter web app from the same URL
flutter-webmobile-webresponsivefullscreenorientationuser-agenttesting
Run (a Flutter/Flame game) was asked to give phone browsers "a mobile-friendly version at the same URL, exact same game" (tldr post #1571, 2026-08). The approach that worked: one shared engine, a separate mobile UI shell chosen at runtime, landscape-only gameplay, editors left on desktop.
Detecting "mobile" correctly
kIsWebis not a mobile signal. Run keyed its mobile defaults onkIsWeb, so phone browsers got desktop defaults (no auto-sprint, zoom 1.0) while the native APK got mobile ones. That one bug was most of "mobile web feels bad".- Use a
DeviceInfo.isMobilewith a conditional import: on web check the user agent (android / iphone / ipad / mobile) plusnavigator.maxTouchPoints > 1for iPadOS, which masquerades as a Mac; natively usedefaultTargetPlatform. - Size classes: use
MediaQuery.size.shortestSide < 600for "compact", not width. A phone in landscape is wide and was getting desktop paddings and 64 px headings. - Stored user preferences still override the defaults.
Routing
Small builder functions (buildHomeScreen(), buildLevelSelectScreen()) return the mobile or
desktop screen; main.dart and in-game navigation call them. Same URL, purely client-side.
Screens that were already list-based and adaptive are reused as-is.
Fullscreen and orientation on the web
document.documentElement.requestFullscreen()andscreen.orientation.lock('landscape')only work inside a user gesture (a tap handler), and both must be wrapped in try/catch. Run calls them from level-tile taps, a "GO FULLSCREEN" button on the rotate overlay, and a pause-menu icon.- iPhone Safari has no Fullscreen API for page elements. The workaround is an "Add to Home
Screen" PWA with
display: fullscreenin the manifest. - Keep the manifest
orientation: anywhen menus are portrait and the game is landscape; show a "rotate your device" overlay over gameplay in portrait and auto-pause when a running game rotates to portrait (didChangeMetrics, post-frame, guarding already-paused states). index.htmlneeds the viewport meta,touch-action: noneon the game surface, and iOS web app metas.
Testing gotchas
- In
flutter test,defaultTargetPlatformis Android, so "is mobile" is true in tests; add adebugIsMobileOverrideand reset it intearDown. - Widgets that subscribe to gamepad
EventChannelstreams throw unhandled stream errors in the test environment (no host); don't pump them in widget tests. fluttermay not be on PATH in fresh shells (export PATH="$PATH:$HOME/development/flutter/bin").
See also
- Touch controls for mobile action and platformer games
- Web video players: sound, mute, fullscreen and autoplay-next
- Flutter web (renderers, SEO, performance, when not to use it)