safe_area_size 0.1.1 copy "safe_area_size: ^0.1.1" to clipboard
safe_area_size: ^0.1.1 copied to clipboard

A plugin to get the SafeArea size using platform channels, making it easy to design around SafeArea's limitations.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:safe_area_size/safe_area_size.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _navigationBarHeight = 0;
  int _statusBarHeight = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    int navigationBarHeight;
    int statusBarHeigth;

    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      statusBarHeigth = await SafeAreaSize.statusBarSize;
      navigationBarHeight = await SafeAreaSize.navigationBarSize;
    } on PlatformException {
      statusBarHeigth = 0;
      navigationBarHeight = 0;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _statusBarHeight = statusBarHeigth;
      _navigationBarHeight = navigationBarHeight;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: Container(
              child: Column(
                children: <Widget>[
                  Expanded(
                    child: Center(
                      child: Text('Top: $_statusBarHeight'),
                    ),
                  ),
                  TextField(
                    textAlign: TextAlign.center,
                    decoration: InputDecoration(
                      hintText: 'Click me and then update!',
                    ),
                  ),
                  MaterialButton(
                    onPressed: () => initPlatformState(),
                    color: Colors.grey,
                    child: Center(child: Text("Update"),),
                  ),
                  Expanded(
                    child: Center(
                      child: Text('Bottom: $_navigationBarHeight'),
                    ),
                  ),
                ],
              ),
            )
          ),
        ),
      ),
    );
  }
}
0
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A plugin to get the SafeArea size using platform channels, making it easy to design around SafeArea's limitations.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on safe_area_size