safe_area_size 0.1.0 safe_area_size: ^0.1.0 copied to clipboard
A plugin to get the SafeArea size using platform channels, making it easy to design around SafeArea's limitations.
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'),
),
),
],
),
)
),
),
),
);
}
}