You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:isekai_wiki/pages/discover.dart';
import 'package:isekai_wiki/pages/home.dart';
import 'package:isekai_wiki/pages/search.dart';
import 'package:isekai_wiki/pages/own_profile.dart';
import 'package:isekai_wiki/styles.dart';
class TabsPageController extends GetxController {
final tabController = CupertinoTabController();
final streamController = StreamController.broadcast();
var tabIndex = 0.obs;
void emit(String eventName) {
streamController.add(eventName);
}
Future<bool> handleWillPop() async {
if (Navigator.canPop(Get.context!)) {
Get.back();
return false;
} else if (tabController.index != 0) {
tabController.index = 0;
return false;
}
return true;
}
void handleTapTab(int tabIndex) {
if (tabIndex != this.tabIndex.value) {
this.tabIndex.value = tabIndex;
return;
}
switch (tabIndex) {
case 0:
emit("tap:home");
break;
}
}
void toSearchPage() {
tabController.index = 2;
}
}
class IsekaiWikiTabsPage extends StatelessWidget {
const IsekaiWikiTabsPage({super.key});
@override
Widget build(BuildContext context) {
var c = Get.put(TabsPageController());
return WillPopScope(
onWillPop: c.handleWillPop,
child: CupertinoTabScaffold(
controller: c.tabController,
tabBar: CupertinoTabBar(
backgroundColor: Styles.themeBottomColor,
activeColor: Styles.themeMainColor,
border: const Border(
top: BorderSide(color: CupertinoColors.systemGrey5, width: 2)),
height: 56,
onTap: c.handleTapTab,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.house),
label: '首页',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.lightbulb),
label: '发现',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
label: '搜索',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.person),
label: '我的',
),
],
),
tabBuilder: (context, index) {
switch (index) {
case 0:
return CupertinoTabView(builder: (context) {
return CupertinoPageScaffold(
child: HomeTab(
onSearchClick: c.toSearchPage,
),
);
});
case 1:
return CupertinoTabView(builder: (context) {
return const CupertinoPageScaffold(
child: DiscoverTab(),
);
});
case 2:
return CupertinoTabView(builder: (context) {
return const CupertinoPageScaffold(
child: SearchTab(),
);
});
case 3:
return CupertinoTabView(builder: (context) {
return const CupertinoPageScaffold(
child: OwnProfileTab(),
);
});
}
return CupertinoTabView(builder: (context) {
return const CupertinoPageScaffold(
child: HomeTab(),
);
});
},
),
);
}
}