Skip to main content

pwa_safe_area 0.1.0 copy "pwa_safe_area: ^0.1.0" to clipboard
pwa_safe_area: ^0.1.0 copied to clipboard

Flutter web fix for iOS home-screen PWAs where env(safe-area-inset-*) reads zero: re-asserts viewport-fit=cover and merges real CSS safe-area insets into MediaQuery.

pwa_safe_area #

Keep Flutter widgets clear of the notch and home indicator in an iOS home-screen web app.

Licence: MIT

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
  1. 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.

  2. In the <head> of web/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>
    
  3. Call setupViewportFit() before runApp and put PwaSafeArea in 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 #

MIT

0
likes
160
points
30
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter web fix for iOS home-screen PWAs where env(safe-area-inset-*) reads zero: re-asserts viewport-fit=cover and merges real CSS safe-area insets into MediaQuery.

Repository (GitHub)
View/report issues

Topics

#pwa #safe-area #ios #web

License

MIT (license)

Dependencies

flutter, web

More

Packages that depend on pwa_safe_area