It's almost 2 years late, but I found my decent solution for this. It's not that simple but pretty usable.
main.dart
inside MaterialApp
, add navigatorObservers
:
MaterialApp(
initialRoute: "/welcome",
routes: <String, WidgetBuilder>{
'/welcome': (BuildContext context) => WelcomeScreen(),
'/login': (BuildContext context) => LoginScreen(),
'/register': (BuildContext context) => RegisterScreen(),
},
// other codes ...
navigatorObservers: [MyRouteObserver()],
// other codes ...
);
and at the very bottom, add:
class MyRouteObserver extends RouteObserver<PageRoute<dynamic>> {
void _setTitle(String what, PageRoute<dynamic> routeFrom, PageRoute<dynamic> routeTo) {
final oldScreenName = routeFrom.settings.name;
final newScreenName = routeTo.settings.name;
log("route changed: $what ($oldScreenName -> $newScreenName)"); // it's just route activity log, don't forget to import 'dart:developer';
final uri = Uri.parse(newScreenName ?? "");
final routeName = uri.pathSegments.isNotEmpty ? uri.pathSegments.first : "";
switch ("/$routeName") {
case "/welcome" : return setTitle("Welcome");
case "/login" : return setTitle("Login");
case "/register" : return setTitle("Sign Up");
// etc ...
}
setTitle("Not Found");
}
@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute && previousRoute is PageRoute) _setTitle("push", previousRoute, route);
}
@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (newRoute is PageRoute && oldRoute is PageRoute) _setTitle("replace", oldRoute, newRoute);
}
@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);
if (route is PageRoute && previousRoute is PageRoute) _setTitle("pop", route, previousRoute);
}
}
helpers.dart
void setTitle([String? title]) async {
Future.microtask(() {
SystemChrome.setApplicationSwitcherDescription(ApplicationSwitcherDescription(
label: "Your APP | ${title ?? 'Your Tagline'}",
primaryColor: Colors.blue.value, // your app primary color
));
});
}
finally, if you want to override title individually in specific page, do:
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((_) {
setTitle("Custom Page Title");
});
}