Bottom inset observer
This package is part of the SurfGear toolkit made by Surf.
Description
BottomInsetObserver - a package for tracking changes of the bottom margin (viewInsets.bottom) using callBack functions.
Installation
Add bottom_inset_observer
to your pubspec.yaml
file:
dependencies:
bottom_inset_observer: $currentVersion$
At this moment, the current version of bottom_inset_observer
is .
Example
As an example, this package can be used to track the presence of a keyboard and handle its visibility status
String _status = '';
bool _isVisible = false;
late BottomInsetObserver _insetObserver;
@override
void initState() {
super.initState();
/// Create instance of the observer and add changes listener
/// Note: if viewInsets.bottom > 0 -> handler called immediately
/// with current inset and delta == 0
_insetObserver = BottomInsetObserver()..addListener(_keyboardHandle);
}
@override
void dispose() {
/// Unregisters the observer observer and remove all listeners
_insetObserver.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
const SizedBox(height: 50),
Text(_isVisible ? 'Visible' : 'Hidden'),
Text('Change status: $_status'),
const SizedBox(height: 50),
const TextField(),
const SizedBox(height: 50),
ElevatedButton(
onPressed: () {
if (FocusManager.instance.primaryFocus != null) {
FocusManager.instance.primaryFocus!.unfocus();
} else {
FocusScope.of(context).requestFocus(FocusNode());
}
},
child: const Text('Reset focus'),
),
ElevatedButton(
onPressed: () {
/// You can remove listener manually
_insetObserver.removeListener(_keyboardHandle);
},
child: const Text('Remove listener'),
),
],
),
);
}
/// Inset change handler
/// Will be called when subscribing if there is an inset > 0,
/// further on every inset change
void _keyboardHandle(BottomInsetChanges change) {
setState(() {
/// getting current inset and check current status
/// of keyboard visability
_isVisible = change.currentInset > 0;
/// get delta since last change and check current status of changes
if (change.delta == 0) _status = 'idle';
if (change.delta > 0) _status = 'increase';
if (change.delta < 0) _status = 'decrease';
});
}
Changelog
All notable changes to this project will be documented in this file.
Issues
To report your issues, submit them directly in the Issues section.
Contribute
If you would like to contribute to the package (e.g. by improving the documentation, fixing a bug or adding a cool new feature), please review our contribution guide first and send us your pull request.
Your PRs are always welcome.
How to reach us
Please feel free to ask any questions about this package. Join our community chat on Telegram. We speak English and Russian.