fix: tabbar rebuild issue

Signed-off-by: Kingtous <kingtous@qq.com>
This commit is contained in:
Kingtous
2022-08-05 10:27:06 +08:00
parent ab6a83e8b0
commit 1977ee951e
3 changed files with 132 additions and 123 deletions

View File

@@ -20,11 +20,12 @@ class FileManagerTabPage extends StatefulWidget {
}
class _FileManagerTabPageState extends State<FileManagerTabPage>
with SingleTickerProviderStateMixin {
with TickerProviderStateMixin {
// refactor List<int> when using multi-tab
// this singleton is only for test
var connectionIds = List<String>.empty(growable: true).obs;
var initialIndex = 0.obs;
var initialIndex = 0;
late Rx<TabController> tabController;
_FileManagerTabPageState(Map<String, dynamic> params) {
if (params['id'] != null) {
@@ -35,6 +36,8 @@ class _FileManagerTabPageState extends State<FileManagerTabPage>
@override
void initState() {
super.initState();
tabController =
TabController(length: connectionIds.length, vsync: this).obs;
rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
print(
"call ${call.method} with args ${call.arguments} from window ${fromWindowId}");
@@ -44,10 +47,15 @@ class _FileManagerTabPageState extends State<FileManagerTabPage>
final id = args['id'];
final indexOf = connectionIds.indexOf(id);
if (indexOf >= 0) {
initialIndex.value = indexOf;
initialIndex = indexOf;
tabController.value.animateTo(initialIndex, duration: Duration.zero);
} else {
connectionIds.add(id);
initialIndex.value = connectionIds.length - 1;
initialIndex = connectionIds.length - 1;
tabController.value = TabController(
length: connectionIds.length,
initialIndex: initialIndex,
vsync: this);
}
} else if (call.method == "onDestroy") {
print("executing onDestroy hook, closing ${connectionIds}");
@@ -65,54 +73,53 @@ class _FileManagerTabPageState extends State<FileManagerTabPage>
@override
Widget build(BuildContext context) {
return Scaffold(
body: Obx(
()=> DefaultTabController(
initialIndex: initialIndex.value,
length: connectionIds.length,
animationDuration: Duration.zero,
child: Column(
children: [
DesktopTitleBar(
child: TabBar(
isScrollable: true,
labelColor: Colors.white,
physics: NeverScrollableScrollPhysics(),
indicatorColor: Colors.white,
tabs: connectionIds
.map((e) => Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(e),
SizedBox(
width: 4,
),
InkWell(
onTap: () {
onRemoveId(e);
},
child: Icon(
Icons.highlight_remove,
size: 20,
))
],
),
))
.toList()),
),
Expanded(
child: TabBarView(
children: connectionIds
.map((e) => Container(
child: FileManagerPage(
key: ValueKey(e),
id: e))) //RemotePage(key: ValueKey(e), id: e))
.toList()),
)
],
body: Column(
children: [
DesktopTitleBar(
child: Obx(
() => TabBar(
controller: tabController.value,
isScrollable: true,
labelColor: Colors.white,
physics: NeverScrollableScrollPhysics(),
indicatorColor: Colors.white,
tabs: connectionIds
.map((e) => Tab(
key: Key('T$e'),
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(e),
SizedBox(
width: 4,
),
InkWell(
onTap: () {
onRemoveId(e);
},
child: Icon(
Icons.highlight_remove,
size: 20,
))
],
),
))
.toList()),
),
),
),
Expanded(
child: Obx(
() => TabBarView(
controller: tabController.value,
children: connectionIds
.map((e) => FileManagerPage(
key: ValueKey(e),
id: e)) //RemotePage(key: ValueKey(e), id: e))
.toList()),
),
)
],
),
);
}
@@ -123,6 +130,8 @@ class _FileManagerTabPageState extends State<FileManagerTabPage>
return;
}
connectionIds.removeAt(indexOf);
initialIndex.value = max(0, initialIndex.value - 1);
initialIndex = max(0, initialIndex - 1);
tabController.value = TabController(
length: connectionIds.length, initialIndex: initialIndex, vsync: this);
}
}