-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
PageStorageKey with TabBarView causes PageController and TabController to get out of sync #10969
Comments
@HansMuller would know. |
Here is a workaround for anyone who is running into this limitation of |
The fix here should be to make sure there's only one source of truth on the currently selected tab, so that however it gets changed, every client changes automatically to match. |
I noticed this bug from 2017. Are we still planning to work on it? |
Please do! This is important. I wonder why Flutter is building beautiful clocks worldwide while simple Material App components are manifested with troublesome bugs. |
Reproducible on latest master flutter doctor -v
|
currently the PageStorageKey doesn’t work in Flutter -.- flutter/flutter#10969
It may be difficult to get the TabBar to support PageStorageKey, because the TabController actually affects two widgets, Tab and TabView, and the TabView contains the PageView, which already supports PageStorageKey, It's going to get messy. So I suggest, if you want the DefaultTabController to support PageStorageKey, you can achieve the same behaviour. I don't know what you think? example code: DefaultTabController(
length: 2,
key: const PageStorageKey<Type>(DefaultTabController),
child: Column(
children: const <Widget>[
TabBar(
tabs: <Widget>[
Tab(text: 'A'),
Tab(text: 'B'),
],
),
Expanded(
child: TabBarView(
children: <Widget>[
Text('C'),
Text('D'),
],
),
),
],
),
),
Actually I've already done the work on the DefaultTabController support for PageStorageKey, so if you approve such a solution I'll PR it right away. |
I wonder if we should just set Doing that would be preferred over integrating PageStorage into the DefaultTabController because PageStorage is not compatible with state restoration. |
@goderbauer Thank you very much and I will try to use the solution you provided. |
@collinjackson Thank you for the workaround. It works fine. However, it cannot be used in a StatelessWidget because it requires a TabController initialized in I think that something like #69702 should be implemented by default. |
Issue is reproducible on the latest versions of flutter recordingsIMG_4422.MP4updated sampleimport 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: MyHomePage(),
),
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
final PageController _controller = PageController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: _controller,
children: <Widget>[
DefaultTabController(
length: 2,
child: Column(
children: <Widget>[
const SizedBox(
height: 200.0,
child: Center(
child: Text('Swipe here'),
),
),
Container(
height: 100.0,
color: Colors.grey,
child: const TabBar(
key: PageStorageKey<Type>(TabBar),
tabs: <Widget>[
Tab(text: 'Green'),
Tab(text: 'Red'),
],
),
),
Expanded(
child: TabBarView(
key: const PageStorageKey<Type>(TabBarView),
children: [
Container(color: Colors.green),
Container(color: Colors.red),
],
),
),
],
),
),
const Center(
child: Text('Ok now swipe back'),
),
],
),
);
}
} flutter doctor -v
|
This issue is assigned to @YeungKC but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks! |
This issue is assigned to @goderbauer but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks! |
This issue was assigned to @goderbauer but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed. |
TabBarView
uses theindex
of itstabController
to set theinitialPage
of itsPageController
. However, thePageController
can later determine a different_pageToUseOnStartup
using itsPageStorage
. There's no mechanism currently in place for theTabBarView
to notify itstabController
that thePageController
has decided to start up on a different page.The result is that you can have a
TabBar
that doesn't match itsTabBarView
, even though they share a commonTabController
. This seems like a bug.Flutter Doctor
The text was updated successfully, but these errors were encountered: