How to show alert on screen when app is in background in flutter
or how to change state of flutter app from pause to resume
hi the answer is to use WidgetsBindingObserver.
in your app just add
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver{}
add the following inside our initState:
WidgetsBinding.instance.addObserver(this);
didChangeAppLifecycleState will be:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}
here is the link of the full article link
I hope it will help you.
Related
How can I listen to an overlay that is inserted in flutter?
I have a main screen which consists of a scroll view and a button which opens an overlay.
When the overlay is opened I want to disable scroll on the below scroll view.
Ive tried using RouteAware but it is only executed when pushing another PageRoute.
The listeners are only executed when I push a new route using the main navigator but ive found no solution on how to listen when an overlay is pushed ontop of the current route.
Code:
class MyWidget extends StatelessWidget {
#override
_MyWidget State createState() => _MyWidget State();
}
class _MyWidgetState extends StatefulWidget<MyWidget> with RouteAware {
bool _scrollDisabled = false;
#override
didChangeDependencies() {
super.didChangeDependencies();
getIt<RouteObserver>().subscribe(this, ModalRoute.of(context) as PageRoute);
}
#override
dispose() {
getIt<RouteObserver>().unsubscribe(this);
super.dispose();
}
didPushNext() {
print("didPushNext");
setState(() {
_scrollDisabled = true;
});
}
didPush() {
print("didPush");
}
}
I've already tried:
#override
void didChangeDependencies() {
super.didChangeDependencies();
Orientation orientation = MediaQuery.of(context).orientation;
(orientation == Orientation.portrait)
? print(orientation)
: Navigator.pop(context);
}
in the _BottomContentState but that gives me an error when I switch the orientation of the device.
Additionally I want the ModalBottomSheet to close if it is open, otherwise no action
You could use an OrientationBuilder widget to programatically close your modal bottom sheet.
OrientationBuilder(
builder: (context, orientation) {
if (orientation == Orientation.landscape) {
// Close your modal
}
}
);
I'm using firebase_admob package in a flutter project, and when I load and show a banner ad on the first screen it's loaded and displayed correctly, but when I navigate to a new screen that doesn't contain a banner, the old banner is still shown on the new screen,
this is the creation code of the banner called in the initState()
BannerAd createBannerAd() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
//Change BannerAd adUnitId with Admob ID
size: AdSize.smartBanner,
targetingInfo: AdsUtil.targetingInfo,
listener: (MobileAdEvent event) {
if (event == MobileAdEvent.loaded) {
// dispose after you received the loaded event seems okay.
if (mounted) {
_bannerAd..show();
setState(() {
isBannerShown = true;
});
} else {
_bannerAd = null;
setState(() {
isBannerShown = false;
});
}
} else if (event == MobileAdEvent.failedToLoad) {
_bannerAd = null;
setState(() {
isBannerShown = false;
});
}
// print("********** ********** BannerAd $event");
});
}
#override
void dispose() {
_bannerAd.dispose();
super.dispose();
}
Navigation to the second screen code :
IconButton(
icon: Icon(Icons.settings),
onPressed: () {
Navigator.of(context).pushNamed(SettingsPage.routeName);
},
),
I want to find a solution that when I navigate to a new screen the old banner get destroyed and not displayed in the new screen I navigated to.
Thanks
When you push a new page then dispose method is not called the native adView remains on the next screen as an overlay widget because it's not part of the widget tree. You need to keep track of pages and then load the banner and dispose it as per page change and reload the ad when the page reloads.
So instead of pushing the page, replacing it with the new page will fix the issue.
You can create RouteObserver and keep track of the page change. Check this answer.
How to check if the keyboard is visible or not in Flutter?
I want to show/hide a widget on the keyboard's visibility state.
Thanks in advance!
keyboard_visibility this package will helps you in detecting the visibility of virtual keyboard in both ios and android.
import 'package:keyboard_visibility/keyboard_visibility.dart';
#protected
void initState() {
super.initState();
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
print(visible);
},
);
}
I would like to detect when flutter app comes back from background.
In other SDKs for crossApp development, there is usually a listener when app changes this state.
Is there anything similar in flutter?
class _AppState extends State<App> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
//add an observer to monitor the widget lyfecycle changes
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
//don't forget to dispose of it when not needed anymore
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
late AppLifecycleState _lastState;
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed && _lastState == AppLifecycleState.paused) {
//now you know that your app went to the background and is back to the foreground
}
_lastState = state; //register the last state. When you get "paused" it means the app went to the background.
}
}