'Flutter SearchDelegate "BACK" button doesn't execute "close()"
I have an appBar with a search button, which when pressed returns a custom DataSearch class that extends SearchDelegate.
When I close the search page (from the device's go back button or from the widget), I need to execute a certain function first. However the function is only executed from the below widget and not when the device's "BACK" button is pressed.
(Image below)
Here's my code:
class DataSearch extends SearchDelegate<String> {
@override
List<Widget> buildActions(BuildContext context) {
// ...
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
function_I_need_to_execute();
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
// ...
}
@override
Widget buildSuggestions(BuildContext context) {
// ...
}
}
I've tried this, but no changes:
@override
void close(BuildContext context, String result) {
super.close(context, result);
function_I_need_to_execute();
}
I'm considering using the WillPopScope widget but I'm not sure where it fits in the delegate.
Image:
Solution 1:[1]
Seems this is not possible using the close method.
However you can do this using the .then method on showSearch since showSearch is a Future.
The then method registers callbacks to be called when this future completes.
showSearch(context: context, delegate: SearchDelegate()).then((value) async{
await doSomething();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mohammad |