pwa_safe_area
Keep Flutter widgets clear of the notch and home indicator in an iOS home-screen web app.
Flutter can drop viewport-fit=cover when its web engine starts. CSS then reports
zero for the safe-area insets, leaving SafeArea with no padding to apply.
This package keeps the viewport setting and brings the CSS insets into MediaQuery.
Quick start
From your Flutter app:
flutter pub add pwa_safe_area
-
Copy the bootstrap file to
web/flutter_bootstrap.js. If you have a custom bootstrap, keep your loader and copy the viewport setup above{{flutter_js}}. It must run before the engine starts. -
In the
<head>ofweb/index.html, use these viewport settings and styles. Replace any existing viewport tag so there is only one.<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <style> html { height: 100vh; width: 100vw; overflow: hidden; } body { margin: 0; } </style> -
Call
setupViewportFit()beforerunAppand putPwaSafeAreain your app's builder. Here's a complete app:import 'package:flutter/widgets.dart'; import 'package:pwa_safe_area/pwa_safe_area.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); setupViewportFit(); runApp(WidgetsApp( color: const Color(0xff202020), builder: (context, _) => const PwaSafeArea( child: SafeArea(child: Text('Inside the safe area')), ), )); }
For an app with a navigator, keep the builder's child:
builder: (context, child) => PwaSafeArea(child: child ?? const SizedBox.shrink()).
Reading the insets
Use SafeArea for layout, or read MediaQuery.paddingOf(context) below the
wrapper. Each edge gets the larger of the CSS inset and Flutter's view padding.
For the raw CSS values, use safeAreaInsets or listen to safeAreaInsetsStream
for changes. The example displays both the CSS and Flutter readings.
Platforms and limits
- The workaround targets web apps opened from the iOS home screen. A browser tab can report zero CSS insets.
- Native builds keep Flutter's own padding. You can use the same wrapper and startup call in a shared app.
- After a Flutter upgrade, open the installed web app on an iPhone and check the insets and tap positions. The bootstrap depends on how the engine measures the viewport. See Flutter issue 84833.
Contributing
Use the Flutter version in .fvmrc. From the package directory:
flutter pub get
(cd example && flutter pub get)
dart format --output=none --set-exit-if-changed lib test example/lib
flutter analyze
flutter test
flutter test --platform chrome test/viewport_web_test.dart
(cd example && flutter build web)
For a layout bug, include your iOS and Flutter versions and say whether you opened the app from the home screen or a browser tab.
Licence
Libraries
- pwa_safe_area
- Safe-area padding for Flutter web apps opened from the iOS home screen.