How to close searchbar after returning a new screen ? Flutter - flutter

class DataSearch extends SearchDelegate<String> {
....
#override
Widget buildResults(BuildContext context) {
return MyProfile();
}
.....
}
this is the code for search delegate
i tried Close() Method but it close the Profile Screen also
when i use navigate method same as image
where should i use close method or am doing it in a wrong way ?

You may try this. Add this at your on search complete
FocusManager.instance.primaryFocus.unfocus();

Related

Flutter GetX why are controllers not auto-deleted when navigating away

Maybe I am misunderstanding something here, but I have followed the docs on https://pub.dev/packages/get to set up a controller for each of my pages.
e.g standard page stuff:
Pages:
PageIntro -> PageLogin -> PageHome etc.
Controllers:
- IntroController
- LoginController
- HomeController
I've defined each of my pages the same way, e.g.:
class PageIntro extends StatelessWidget {
#override
Widget build(BuildContext context) {
final IntroController c = Get.put(IntroController(context));
return Scaffold(
body: _buildContent(), // you get the idea.
);
}
}
I was expecting that when navigating away from PageIntro to PageLogin, that the IntroController would be deleted, but it's not.
I know I can manually delete controllers with:
Get.delete<IntroController>();
But I shouldn't have to.
Unless I'm missing something/doing something really stupid, this seems more hassle, more messy that just having PageIntro extend StatefulWidget, then doing whatever I need to do in the dispose override method as usual.
Please can someone more knowledgable on this inform me?
Thanks.
In your case, binding is the way to go.
On the other hand, controllers are removed when the route is removed from the stack. In your case, you maybe pushing to the next screen rather than replacing it.
I think it will help you to avoid this error. You should call in inside of build method.
#override
Widget build(BuildContext context) {
var controller = ControllerStateDispatcher<RecentScreenController>(
RecentScreenController(),true) // if you want to save state, false if you want reset state)
.get<RecentScreenController>();
}
abstract class Cleaner {
void clear();
}
abstract class GetControllerProvider<T extends GetxController> {
R get<R extends GetxController>();
}
class ControllerStateDispatcher<T extends GetxController> extends Cleaner
implements GetControllerProvider {
bool shouldSaveState;
T controller;
ControllerStateDispatcher(this.controller, [this.shouldSaveState = false]);
#override
void clear() {
Get.delete<T>();
}
#override
DispatcherController get<DispatcherController extends GetxController>() {
if (!shouldSaveState) clear();
controller = Get.put(controller);
return controller as DispatcherController;
}
}

How to bypass a screen using navigator in flutter?

I am coming from android.
In android ,if i want to bypass an activity i will call intent method in onCreate something like this.
onCreate(){
if(condition satisfied){
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
}
But i cant understand how to write this code in flutter and navigate another screen .
And I also don't know that this code will write in initState() or build().
note: In this case in If condition, I used StreamBuilder,
I need in following steps,
At screen start
Blank screen
if Condition will be true than return Container();
else leave this Screen(Activity) go to another screen.
Use this :
return isSomethingtrue ? HomeScreen() : OtherScreen(),
Then - In your HomeScreen() Build a Container()
class HomeScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container();
}
}

How can I execute a function when a user hits the back button or swipes to close a screen in flutter?

I want to always execute a function when a screen is closed, either when a user presses the back button or does a swipe to close the page.
What I have tried is to use WillPopScope
return WillPopScope(
onWillPop: _onWillPop,
child: CupertinoScaffold( ... the rest of my tree
Future<bool> _onWillPop() async {
myFunction();
return true;
}
While this does successfully execute my function whenever the page is closed via the back button, it seems to have disabled being able to swipe to go back on iOS. Swipe to go back on android still seems to function.
Is there another way of doing this so I can retain the swipe to go back functionality on iOS?
edit:
Just incase it matters, myFunction() involves a provider and context and throws an error when I try to call it from dispose.
In actuality myFunction() is:
context.read(myVisibilityProvider).changeVisibility(false);
I successfully managed to run a function calling it from dispose retaining the iOS swipe to back gesture.
class ScrondScreen extends StatefulWidget {
const ScrondScreen({Key? key}) : super(key: key);
#override
_ScrondScreenState createState() => _ScrondScreenState();
}
class _ScrondScreenState extends State<ScrondScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
);
}
void myFunction() {
print('I run');
}
#override
void dispose() {
myFunction();
super.dispose();
}
}
console:
flutter: I run

Show Snackbar in any screen when task finishes

I have a task that will run in the background (in an isolate) and when it finishes, I want to show a Snackbar. However, the user may navigate to a different screen from the one where the task was initiated. How do I show an 'app-level' Snackbar, not bound to any particular screen?
Edit: I found this: How to show a SnackBar from async executions when no context is available?, has some good information and option 1 (Display errors in page scaffolds) seems to be what I want, but I need to implement all by myself. I was hoping for something built in into Flutter.
I'd give this a try where you go for an abstract base class where you implement the 'listener', which then all your pages extend from instead of e.g. StatelessWidget.
Instead of overriding the normal build method, just override your new special build.
Pseudo code (here with BlocListener):
abstract class MySnackbarShowingPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return BlocListener<SubjectBloc, SubjectState>(
listener: (context, state) {
// TODO: implement snackbar display
},
child: mySpecialBuild(context),
);
}
Widget mySpecialBuild(BuildContext context);
}
class MySpecificPage extends MySnackbarShowingPage {
#override
Widget mySpecialBuild(BuildContext context) {
// TODO: implement mySpecialBuild as your normal page does today
}
}

How to add an animation each time Widget build(BuildContext context) is called in Flutter

Here is my build method:
#override
Widget build(BuildContext context) {
if (isAuth) {
return buildAuthScreen();
} else if (isRegister) {
return buildRegisterUnAuthScreen();
} else {
return buildUnAuthScreen();
}
}
I wanted to know if there is way to call an animation each time my build function is called and a new page is returned, so that the new page is returned with an animation.
I already know, that there is a way to do it with Animation Page route, but I only want to display an animation when displaying a new buildScreen.
Thank you.
You should not start animation when build method is called. The build method may be called at every frame.
To start some side effect when widget property is changed, you should use StatefulWidget and initState and didUpdateWidget methods.