Skip to content

Commit

Permalink
fix: 节点排序后首页tabview未刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Mar 9, 2023
1 parent 995e9c7 commit f15c907
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
4 changes: 3 additions & 1 deletion lib/components/home/sticky_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_v2ex/models/tabs.dart';

class HomeStickyBar extends StatelessWidget {
const HomeStickyBar({super.key, required this.tabs});
const HomeStickyBar({super.key, required this.tabs, required this.ctr});
final List<TabModel> tabs;
final TabController ctr;
@override
Widget build(BuildContext context) {
return SizedBox(
Expand All @@ -13,6 +14,7 @@ class HomeStickyBar extends StatelessWidget {
children: [
Expanded(
child: TabBar(
controller: ctr,
dividerColor: Colors.transparent,
onTap: (index) {},
isScrollable: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/http/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Request {
return 'PROXY localhost:7890';
// return 'PROXY 127.0.0.1:7890';
// 不设置代理 TODO 打包前关闭代理
return 'DIRECT';
// return 'DIRECT';
};
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
Expand Down
89 changes: 61 additions & 28 deletions lib/pages/page_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,22 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage>
with AutomaticKeepAliveClientMixin {
with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
// 自定义、 缓存 、 api获取
List<TabModel> tabs = GStorage()
.getTabs()
.where((item) => item.checked)
.toList();
String shortcut = 'no action set';
late TabController _tabController = TabController(vsync: this, length: tabs.length);


@override
void initState() {
// TODO: implement initState
super.initState();
eventBus.on('editTabs', (args) {
setState(() {
tabs = GStorage()
.getTabs()
.where((item) => item.checked)
.toList();
});
_loadCustomTabs();
});
const QuickActions quickActions = QuickActions();
quickActions.initialize((String shortcutType) {
Expand Down Expand Up @@ -81,6 +78,20 @@ class _HomePageState extends State<HomePage>
});
}

void _loadCustomTabs() {
var customTabs = GStorage()
.getTabs()
.where((item) => item.checked)
.toList();

setState(() {
tabs.clear();
tabs.addAll(customTabs);
_tabController = TabController(length: tabs.length, vsync: this);
/// DefaultTabController在build外无法重新build tabView
// DefaultTabController.of(context).animateTo(0);
});
}
// 页面缓存
@override
bool get wantKeepAlive => true;
Expand All @@ -90,27 +101,49 @@ class _HomePageState extends State<HomePage>
super.build(context);
num height = MediaQuery.of(context).padding.top;
GStorage().setStatusBarHeight(height);
return DefaultTabController(
length: tabs.length,
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const HomeSearchBar(),
),
drawer: const HomeLeftDrawer(),
body: Column(
children: <Widget>[
HomeStickyBar(tabs: tabs),
const SizedBox(height: 3),
Expanded(
child: TabBarView(
children: tabs.map((e) {
return TabBarList(e);
}).toList(),
),
)
],
),
/// DefaultTabController在build外无法重新build tabView
// return DefaultTabController(
// length: tabs.length,
// child: Scaffold(
// appBar: AppBar(
// automaticallyImplyLeading: false,
// title: const HomeSearchBar(),
// ),
// drawer: const HomeLeftDrawer(),
// body: Column(
// children: <Widget>[
// HomeStickyBar(tabs: tabs),
// const SizedBox(height: 3),
// Expanded(
// child: TabBarView(
// children: tabs.map((e) {
// return TabBarList(e);
// }).toList(),
// ),
// )
// ],
// ),
// ),
// );
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const HomeSearchBar(),
),
drawer: const HomeLeftDrawer(),
body: Column(
children: <Widget>[
HomeStickyBar(tabs: tabs, ctr: _tabController),
const SizedBox(height: 3),
Expanded(
child: TabBarView(
controller: _tabController,
children: tabs.map((e) {
return TabBarList(e);
}).toList(),
),
)
],
),
);
}
Expand Down

0 comments on commit f15c907

Please sign in to comment.