Remove text field soft focus WillPopScope - flutter

everything is fine to remove the focus from text field except for WillPopScope function, after i press back button the keyboard dismiss but the focus line still in the textfield.
let me know if you need more information about the code
removeFocus Code
void removeFocus() {
FocusManager.instance.primaryFocus?.unfocus();
}
WillPopScope Code
onWillPop: () async {
removeFocus();
return true;
},
i mean this blue line won't dismiss

This is what I am using in my project:
onWillPop: () async {
FocusScope.of(context).requestFocus(FocusNode());
return true;
},

Change your removeFocus() method as the following:
void removeFocus() {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
}

Related

Flutter- App content disappear when NotificationListener sends notification for first time

I am creating an app where i am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time all the content gets disappear for 2-3 seconds...I am using Provider.... Please help me...
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a link to 14 seconds short video
enter link description here

Flutter NotificationListener crashed app for some seconds

I am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time when app starts all the content gets disappear for some seconds...I am using Provider,Please help me... It only happens for the very first time
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a video
Video

I need to hide my FloatingActionButton when scrolling a SingleChildScrollView

I need to hide my FloatingActionButton when scrolling a SingleChildScrollView. I found something about how to hide it when the user scrolls down and then show it when the user scrolls up, but that's not what I'm looking for.
I want the button to remain hidden only during the scroll. I also tried playing with the NotificationListener but didn't come up with a solution
Hide until you don't get any more notifications.
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
if (_show) setState(() => _show = false);
_timer?.cancel();
_timer = Timer(const Duration(milliseconds: 200), () => setState(() => _show = true));
return true;
},
child: <your widget>
)

Flutter Not able to close keyboard on app start if it's visible for any reason when the app launches

Not able to dismiss the keyboard on app start. A demo is given below.
have tried following in the build method of the first widget of MaterialApp after launch and in the initState method but non worked:
1.
final FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus!.unfocus();
}
final FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
FocusScope.of(context).unfocus();
try to wrap your bg with GestureDetector and try it
onTap: ()=>FocusManager.instance.primaryFocus?.unfocus()
This works for me. I only tested in android.
SystemChannels.textInput.invokeMethod('TextInput.hide');

Flutter firebase_admob banner is still shown on every screen

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.