'Hide keyboard on didChangeAppLifecycleState in flutter
How to hide keyboard on didChangeAppLifecycleState
I created a widget called lifecycle_widget and wrapped app with so I can listen to app lifecycle state changes
here is my lifecycle widget:
class LifeCycle extends StatefulWidget {
final Widget child;
const LifeCycle({required this.child});
@override
_LifeCycleState createState() => _LifeCycleState();
}
class _LifeCycleState extends State<LifeCycle> with WidgetsBindingObserver {
@override
Widget build(BuildContext context) {
return GestureDetector(child: widget.child,
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
currentFocus.focusedChild!.unfocus();
}
SystemChannels.textInput.invokeMethod('TextInput.hide');
},
);
}
@override
void initState() {
WidgetsBinding.instance?.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
print('state = $state');
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
currentFocus.focusedChild!.unfocus();
}
SystemChannels.textInput.invokeMethod('TextInput.hide');
}
}
and here how I wrapped my app:
@override
Widget build(BuildContext context) {
var localizationDelegate = LocalizedApp.of(context).delegate;
return LifeCycle(child: LocalizationProvider(
state: LocalizationProvider.of(context).state,
child: DismissKeyboard(child: GetMaterialApp(
title: 'FastFill',
onGenerateRoute: AppRouter.generateRoute,
debugShowCheckedModeBanner: false,
localizationsDelegates: [
localizationDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
],
supportedLocales: localizationDelegate.supportedLocales,
locale: _locale,
theme: ThemeData(
primaryColor: primaryColor1,
accentColor: primaryColor2,
fontFamily: isArabic() ? 'Poppins' : 'Poppins'),
initialRoute: (isSigned) ? HomePage.route : LanguagePage.route,
))));
}
But it does not work and does not do the job ! when app goes into background and then back to it the keyboard stays and does not response!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
