New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Browser title does not update when navigating back from page #53568
Comments
/cc @goderbauer please triage: Instead it should call it in a TitleRenderObject paint, otherwise the widget won't get rebuild causing this issue. |
Moving the call to paint doesn't fully work. Imagine you have a repaint boundary, which has a Title widget surrounding it and another one as a child. If the one inside the repaint boundary goes away, the one outside the boundary doesn't necessarily repaint and therefore the title set by the no-longer existing Title widget inside the boundary would persist. Looking at the History of the Title widget, it looks like it was designed to be fairly static: Its initial use was to provide a name shown in Android's app switcher, which is usually constant throughout the lifetime of an app. For the reasons outlined in this bug, the title widget doesn't really support nesting (or having title widgets as siblings). We'd probably have to come up with a different mechanism to support the use case implied in this bug (I am assuming the goal is that different Routes on the Navigator should be able to have different page titles?). If we want to continue using the the Title widget for this, we'd have to define what it means to nest them (I would assume the most deeply nested one should "win") and what it means to have them as siblings (e.g. two widgets in a Row or Column each have Title widgets). As a workaround for the original issue one could probably write a Navigator observer that keeps track of the topmost page on the routing stack and calls Alternatively, in the Navigator 2.0 design the RouterDelegate (who is configuring what's shown on the Navigator Stack) could call this API whenever it changes the configuration of the Navigator. (cc @chunhtai FYI) |
Another idea for how the Title widget could be changed to support these use cases: Title widgets are stateful and when they are created, they register their title string with a central registration point. The registration point keeps titles in a stack-like data structure. At the end of each frame the topmost title string is send to the operating system via Edit: Although, this would brake if you add a route below the topmost one as its title now would be newer. Makes me believe that making the RouterDelegate be in charge of this may be the better option... |
Yes. something central like a stack holding mounted/unmounted TitleElements would probably work well. The latest mounted title could take precedence. You can assume only one Title is really active but animating between routes would temporarily have 2. |
Even though I suggested it above, the stack based on mounted/unmounted doesn't work because when you insert a route below the current route, that new route's title would take precedence over the current route. Here's another idea that may work better: We introduce a new RouteTitle widget. It can be used within a Route and registers its title with the surrounding ModalRoute by reaching out to it via At the same time, we should add asserts to ensure that there's always only one Title widget in the tree. This could be done by giving all title widgets the same static global key. That way there can never be more than one in the tree. At the same time we should modify GlobalKey to take an optional error message that should be printed if this particular key ends up duplicated in the tree. This will allow us to print a more actionable error message for this case. |
code sampleimport 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Title Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/test': (context) => Page(title: 'Test Page'),
},
);
}
}
class Page extends StatelessWidget {
const Page({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Title(
title: title,
color: Colors.black,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(title),
RaisedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Go back!'),
),
],
),
),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Title(
title: 'Home Page',
color: Colors.black,
child: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(context).pushNamed('/test');
},
child: Text('Test page'),
),
),
),
);
}
}
flutter doctor -v
|
Chrome tab only shows 'Home', navigate to other page, title doesn't change.
flutter doctor -v
|
having the same issue |
try use js document.title get this ,is working ! |
not sure what you mean here. How do we do this? |
same here, any actual fix rather than change build/web/main.dart.js every time after build? |
Reproducible on the latest versions of flutter. recordingScreen.Recording.2023-03-09.at.12.48.25.movcode sampleimport 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Title Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/test': (context) => const Page(title: 'Test Page'),
},
);
}
}
class Page extends StatelessWidget {
const Page({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Title(
title: title,
color: Colors.black,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(title),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Go back!'),
),
],
),
),
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Title(
title: 'Home Page',
color: Colors.black,
child: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pushNamed('/test');
},
child: const Text('Test page'),
),
),
),
);
}
} flutter doctor -v
|
Reproducible on flutter 3.10.5 in stable branch and on flutter 3.11.0 in master branch. |
Any news on this? |
I found a way to solve this problem, using lifecycle Just update the state when the page is active|resumed. @override
void onLifecycleEvent(LifecycleEvent event) {
if (event == LifecycleEvent.active) {
setState(() {});
}
} Check the package for more details. |
is there any fix for this? I'm having the same issue |
Because you pop route, so there are no Title rebuilt, then it cannot update page title. |
maybe package pagetitle can help |
The navigation widget is used, the problem comes to turn back to previous one. |
Steps to Reproduce
Logs
The text was updated successfully, but these errors were encountered: