diff --git a/lib/components/home/sticky_bar.dart b/lib/components/home/sticky_bar.dart index b4f26c7..5cda0f9 100644 --- a/lib/components/home/sticky_bar.dart +++ b/lib/components/home/sticky_bar.dart @@ -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 tabs; + final TabController ctr; @override Widget build(BuildContext context) { return SizedBox( @@ -13,6 +14,7 @@ class HomeStickyBar extends StatelessWidget { children: [ Expanded( child: TabBar( + controller: ctr, dividerColor: Colors.transparent, onTap: (index) {}, isScrollable: true, diff --git a/lib/http/init.dart b/lib/http/init.dart index 297a5c3..63e9113 100644 --- a/lib/http/init.dart +++ b/lib/http/init.dart @@ -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; diff --git a/lib/pages/page_home.dart b/lib/pages/page_home.dart index 24a0b2e..8f979dc 100644 --- a/lib/pages/page_home.dart +++ b/lib/pages/page_home.dart @@ -18,25 +18,22 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State - with AutomaticKeepAliveClientMixin { + with AutomaticKeepAliveClientMixin, TickerProviderStateMixin { // 自定义、 缓存 、 api获取 List 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) { @@ -81,6 +78,20 @@ class _HomePageState extends State }); } + 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; @@ -90,27 +101,49 @@ class _HomePageState extends State 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: [ - 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: [ + // 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: [ + HomeStickyBar(tabs: tabs, ctr: _tabController), + const SizedBox(height: 3), + Expanded( + child: TabBarView( + controller: _tabController, + children: tabs.map((e) { + return TabBarList(e); + }).toList(), + ), + ) + ], ), ); }