Skip to content

Commit

Permalink
feat(#708): remove focus on route change
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 21, 2024
1 parent 3051680 commit a069345
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 14 additions & 1 deletion app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class PharMeApp extends StatelessWidget {
child: MaterialApp.router(
debugShowCheckedModeBanner: false,
routeInformationParser: _appRouter.defaultRouteParser(),
routerDelegate: _appRouter.delegate(deepLinkBuilder: getInitialRoute),
routerDelegate: _appRouter.delegate(
deepLinkBuilder: getInitialRoute,
navigatorObservers: () => [RemoveFocusOnNavigate()],
),
theme: PharMeTheme.light,
localizationsDelegates: [
AppLocalizations.delegate,
Expand All @@ -36,3 +39,13 @@ class PharMeApp extends StatelessWidget {
);
}
}

// Based on https://github.com/flutter/flutter/issues/48464#issuecomment-586635827
class RemoveFocusOnNavigate extends NavigatorObserver {
@override
void didPush(Route route, Route? previousRoute) {
super.didPush(route, previousRoute);
final focus = FocusManager.instance.primaryFocus;
focus?.unfocus();
}
}
9 changes: 6 additions & 3 deletions app/lib/common/widgets/page_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Scaffold pageScaffold({
);
}

void _maybeRemoveFocus(BuildContext? contextToDismissFocusOnTap) =>
contextToDismissFocusOnTap != null
? FocusScope.of(contextToDismissFocusOnTap).unfocus()
: null;

Widget unscrollablePageScaffold({
required Widget body,
String? title,
Expand All @@ -86,9 +91,7 @@ Widget unscrollablePageScaffold({
titleSpacing: _getTitleSpacing(backButtonPresent: canNavigateBack),
);
return GestureDetector(
onTap: () => contextToDismissFocusOnTap != null
? FocusScope.of(contextToDismissFocusOnTap).unfocus()
: null,
onTap: () => _maybeRemoveFocus(contextToDismissFocusOnTap),
child: Scaffold(
key: key,
appBar: appBar,
Expand Down

0 comments on commit a069345

Please sign in to comment.