feat: 首页tabbar 编辑排序

This commit is contained in:
guozhigq
2024-01-21 23:49:32 +08:00
parent dad4a28eb8
commit e052c6eafe
7 changed files with 168 additions and 62 deletions

View File

@ -8,12 +8,13 @@ import 'package:pilipala/utils/storage.dart';
class HomeController extends GetxController with GetTickerProviderStateMixin {
bool flag = false;
late List tabs;
late RxList tabs = [].obs;
RxInt initialIndex = 1.obs;
late TabController tabController;
late List tabsCtrList;
late List<Widget> tabsPageList;
Box userInfoCache = GStrorage.userInfo;
Box settingStorage = GStrorage.setting;
RxBool userLogin = false.obs;
RxString userFace = ''.obs;
var userInfo;
@ -21,6 +22,8 @@ class HomeController extends GetxController with GetTickerProviderStateMixin {
late final StreamController<bool> searchBarStream =
StreamController<bool>.broadcast();
late bool hideSearchBar;
late List defaultTabs;
late List<String> tabbarSort;
@override
void onInit() {
@ -28,34 +31,10 @@ class HomeController extends GetxController with GetTickerProviderStateMixin {
userInfo = userInfoCache.get('userInfoCache');
userLogin.value = userInfo != null;
userFace.value = userInfo != null ? userInfo.face : '';
// 进行tabs配置
tabs = tabsConfig;
tabsCtrList = tabsConfig.map((e) => e['ctr']).toList();
tabsPageList = tabsConfig.map<Widget>((e) => e['page']).toList();
tabController = TabController(
initialIndex: initialIndex.value,
length: tabs.length,
vsync: this,
);
setTabConfig();
hideSearchBar =
setting.get(SettingBoxKey.hideSearchBar, defaultValue: true);
// 监听 tabController 切换
tabController.animation!.addListener(() {
if (tabController.indexIsChanging) {
if (initialIndex.value != tabController.index) {
initialIndex.value = tabController.index;
}
} else {
final int temp = tabController.animation!.value.round();
if (initialIndex.value != temp) {
initialIndex.value = temp;
tabController.index = initialIndex.value;
}
}
});
}
void onRefresh() {
@ -77,4 +56,42 @@ class HomeController extends GetxController with GetTickerProviderStateMixin {
if (val) return;
userFace.value = userInfo != null ? userInfo.face : '';
}
void setTabConfig() async {
defaultTabs = tabsConfig;
tabbarSort = settingStorage.get(SettingBoxKey.tabbarSort,
defaultValue: ['live', 'rcmd', 'hot', 'bangumi']);
tabs.value = defaultTabs
.where((i) => tabbarSort.contains((i['type'] as TabType).id))
.toList();
if (tabbarSort.contains(TabType.rcmd.id)) {
initialIndex.value = tabbarSort.indexOf(TabType.rcmd.id);
} else {
initialIndex.value = 0;
}
tabsCtrList = tabs.map((e) => e['ctr']).toList();
tabsPageList = tabs.map<Widget>((e) => e['page']).toList();
tabController = TabController(
initialIndex: initialIndex.value,
length: tabs.length,
vsync: this,
);
// 监听 tabController 切换
tabController.animation!.addListener(() {
if (tabController.indexIsChanging) {
if (initialIndex.value != tabController.index) {
initialIndex.value = tabController.index;
}
} else {
final int temp = tabController.animation!.value.round();
if (initialIndex.value != temp) {
initialIndex.value = temp;
tabController.index = initialIndex.value;
}
}
});
}
}

View File

@ -90,7 +90,11 @@ class _HomePageState extends State<HomePage>
ctr: _homeController,
callback: showUserBottomSheet,
),
const CustomTabs(),
if (_homeController.tabs.length > 1) ...[
const CustomTabs(),
] else ...[
const SizedBox(height: 6),
],
Expanded(
child: TabBarView(
controller: _homeController.tabController,
@ -250,17 +254,6 @@ class CustomTabs extends StatefulWidget {
class _CustomTabsState extends State<CustomTabs> {
final HomeController _homeController = Get.put(HomeController());
int currentTabIndex = 1;
@override
void initState() {
super.initState();
_homeController.tabController.addListener(listen);
}
void listen() {
_homeController.initialIndex.value = _homeController.tabController.index;
}
void onTap(int index) {
feedBack();
@ -271,34 +264,30 @@ class _CustomTabsState extends State<CustomTabs> {
_homeController.tabController.index = index;
}
@override
void dispose() {
super.dispose();
_homeController.tabController.removeListener(listen);
}
@override
Widget build(BuildContext context) {
return Container(
height: 44,
margin: const EdgeInsets.only(top: 4),
child: ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 14.0),
scrollDirection: Axis.horizontal,
itemCount: _homeController.tabs.length,
separatorBuilder: (BuildContext context, int index) {
return const SizedBox(width: 10);
},
itemBuilder: (BuildContext context, int index) {
String label = _homeController.tabs[index]['label'];
return Obx(
() => CustomChip(
onTap: () => onTap(index),
label: label,
selected: index == _homeController.initialIndex.value,
),
);
},
child: Obx(
() => ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 14.0),
scrollDirection: Axis.horizontal,
itemCount: _homeController.tabs.length,
separatorBuilder: (BuildContext context, int index) {
return const SizedBox(width: 10);
},
itemBuilder: (BuildContext context, int index) {
String label = _homeController.tabs[index]['label'];
return Obx(
() => CustomChip(
onTap: () => onTap(index),
label: label,
selected: index == _homeController.initialIndex.value,
),
);
},
),
),
);
}