Flutter: How to call function of Home button (home button default of Android)? - flutter

I have a button on the View and how to call function of Home button in onClick function of button?
Code example:
InkWell(
onTap: () {
//function of home button
},)

You could use this package:
https://pub.dev/packages/move_to_background
Meaning your code could be something like this:
InkWell(
onTap: () {
MoveToBackground.moveTaskToBack();
},
)

For what you are trying to achieve you need a background notifier because the home button sends app to background. There is a package called flutter_fgbg which can help you - click here pub.dev. Then you can proceed to stream background events, eg:
import 'package:flutter_fgbg/flutter_fgbg.dart';
StreamSubscription<FGBGType> subscription;
...
// in initState
subscription = FGBGEvents.stream.listen((event) {
if (event == FGBGType.foreground){
//if its come back to foreground, then restart your app
} else {
//if its gone to background
}
});
// in dispose
subscription.cancel();
For more assistance, you can check this link, for visual aid in solving problems Mufungo Geeks YouTube

Related

How can I change or override android/ios back button's action in flutter

I need to make a change in my page (changing a bool in setState etc...) when the user presses the back button.
I looked it up and I know people use WillPopScope to override the default "back" action, but in all of the examples they were just popping the page, and I need to do a custom function instead of that, and have no Idea how to do so.
my function looks like this, and I need to run in when the user pressed the back button:
Future<void> backToCats() async{
setState(() {
isLoaded=false;
});
if(isFromSearch){
loadCategories();
}
setState(() {
showingBrands=false;
isLoaded=true;
});
}
You can perform custom logic in the onWillPop argument of a WillPopScope widget. Just make sure to return true or false at the end to indicate whether the page is allowed to pop. WillPopScope will activate for the back button, and other actions that automatically navigate back such as swiping from the side of the screen on iOS.
For example, if you need to set a bool to false in your stateful widget, it would look something like this
WillPopScope(
onWillPop: () async {
setState(() => myBool = false);
return true;
}
child: ...
);

How can I call a function as soon as I go back to the main screen? - Flutter

Hello,
From the main screen of my app, pressing a button switches to another screen.
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return screen2();
},
),
);
},
When I am in screen2 and I want to go back or I can press a button.
Navigator.pop(context); or press the android back button.
How can I call a function as soon as I go back to the main screen?
Thank you.
From what I understood you want to trigger a function when you go to the previous page!
So,
Make the main screen as a Stateful widget and then initialize an init state and inside the init state put the function you want to trigger
Such as like this below code
#override
void initState() {
// TODO: implement initState
super.initState();
FunctionYouWantToTrigger();
}

Riverpod - How to refresh() after an async function completes after the user has already left the screen

I have a Flutter app that has a screen with a ListView of items that is supplied by a provider via an API call. Tapping an item goes to a second "detail" screen with a button (onPressed seen below) that will make an API call to complete an action. On success, the item is removed from the list by running refresh() to make the first API call again to get an updated list. However, if the user navigates away from the detail screen before the action is completed, executing the refresh is no longer possible and will throw the "Looking up a deactivated widget's ancestor is unsafe" exception. This is why I am first checking the mounted property, but is there any way in this scenario to execute the refresh? I am trying to avoid the confusion of the user seeing an outdated list and having to manually refresh. Thanks.
onPressed: () async {
setState(() {
// Disable the submit button
});
someAsyncFunction().then(
(success) {
if (!mounted) {
return;
}
if (success) {
// Success snackBar
ref.refresh(someProvider);
Navigator.pop(context);
} else {
// Failure snackBar
setState(() {
// Re-enable submit button
});
}
},
);
}

Continu my music after a pause in just_audio plugin

I use the plugin just_audio
How can I continu my music after to do a pause? please.
void _play() {
audioPlayer.setAsset("assets/applause.mp3");
audioPlayer.play();
}
void _pause() async {
await audioPlayer.pause();
}
void _rePlay() async {
?????????????????????????
}
In general, after calling pause, call play to "resume".
In your case, you should NOT set the asset file again. This resets the process.
Below is your code, pasted from your question:
void _play() { // BAD EXAMPLE
audioPlayer.setAsset("assets/applause.mp3"); // <-- PROBLEM
audioPlayer.play();
}
You should move setAsset call elsewhere, like in init state.
You can just call audioPlayer.play(). This works exactly like the play button in a music app, while pause() works exactly like the pause button in a music app.
It is designed this way so that you can simply wire up your play and pause buttons directly to these player methods.
For example, here are the play/pause buttons from the just_audio example app:
// Play button:
IconButton(
icon: Icon(Icons.play_arrow),
onPressed: audioPlayer.play,
)
// Pause button:
IconButton(
icon: Icon(Icons.pause),
onPressed: audioPlayer.pause,
)
You can just alternate calls to play()/pause()/play()/pause(). Note the left side of this state diagram from the README:

Flutter - Alert Dialog showing multiple times inside listener

I am using a flutter_local_notifications to get a notification when a countdown timer has finished.
When i click the notification using onSelectNotification (callback of flutter_local_notification), I add a Subject (RxDart).
var selectNotificationSubject = PublishSubject<String>();
onSelectNotification: (String payload) async {
selectNotificationSubject.add(payload);
},
So the Subject is added successfully on the Stream.
I listen for the stream in initState. The AlertDialog is shown, but the problem is that it is shown multiple times for all the list with countdown timers.
I want to show the AlertDialog only for the timer that i clicked on the notification, not for all the timers that are available on the list.
A way to fix this may be to add to the AlertDialog widget a id so i can show it only once by the id of the countdown timer that is clicked on notification. Widget showDialogById()
subs = selectNotificationSubject.stream.listen((event) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Repeat Timer ${timer.id}"),
content: Text(
"Do you want to repeat your timer ${timer.description} ?"),
actions: [
RaisedButton(
child: Text("Repeat"),
onPressed: () async {
//Repeat timer logic here
});
),
});
});
The way you coded it, it is expected to spam repeatedly to show the dialog.
If you don't want a new trigger dialog show, create a boolean to control it, whatever the bool is true it won't show, otherwise it will show