_layout.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { useEffect, useState } from 'react';
  2. import { Tabs, useSegments } from 'expo-router';
  3. import { Dimensions, Platform } from 'react-native';
  4. import { TabAccountIcon, TabChargingIcon, TabHomeIcon } from '../../../component/global/SVG';
  5. import { useColorScheme } from 'nativewind';
  6. export default function TabLayout() {
  7. const { height } = Dimensions.get('window');
  8. const { colorScheme } = useColorScheme();
  9. const activeColor = colorScheme === 'dark' ? '#36DFFF' : '#02677D';
  10. const inactiveColor = colorScheme === 'dark' ? '#666666' : '#BBBBBB';
  11. const segment = useSegments();
  12. const [tabBarVisible, setTabBarVisible] = useState(true);
  13. // Calculate marginBottom based on screen height
  14. const calculateMarginBottom = () => {
  15. if (Platform.OS === 'ios') return 0;
  16. if (height < 600) return 0;
  17. return 15;
  18. };
  19. useEffect(() => {
  20. // Determine if the tab bar should be visible based on the current segment
  21. const isScanQrPage = segment[segment.length - 1] === 'scanQrPage';
  22. setTabBarVisible(!isScanQrPage);
  23. }, [segment]);
  24. return (
  25. <Tabs
  26. screenOptions={{
  27. headerShown: false,
  28. tabBarLabelStyle: {
  29. fontSize: 16,
  30. marginBottom: calculateMarginBottom(),
  31. color: colorScheme === 'dark' ? '#CCCCCC' : '#666666'
  32. },
  33. tabBarStyle: {
  34. height: height * 0.105,
  35. display: tabBarVisible ? 'flex' : 'none',
  36. backgroundColor: colorScheme === 'dark' ? '#041316' : '#FFFFFF'
  37. },
  38. tabBarIconStyle: {
  39. width: 10,
  40. height: 10,
  41. marginTop: 15,
  42. marginBottom: 15
  43. }
  44. }}
  45. >
  46. <Tabs.Screen
  47. name="(home)"
  48. options={{
  49. title: '主頁',
  50. // tabBarHideOnKeyboard: true,
  51. tabBarIcon: ({ focused }) => <TabHomeIcon color={focused ? activeColor : inactiveColor} />
  52. }}
  53. />
  54. <Tabs.Screen
  55. name="(charging)"
  56. options={{
  57. title: '充電',
  58. tabBarHideOnKeyboard: true,
  59. tabBarIcon: ({ focused }) => <TabChargingIcon color={focused ? activeColor : inactiveColor} />
  60. }}
  61. />
  62. <Tabs.Screen
  63. name="(account)"
  64. options={{
  65. title: '帳戶',
  66. tabBarHideOnKeyboard: true,
  67. tabBarIcon: ({ focused }) => <TabAccountIcon color={focused ? activeColor : inactiveColor} />
  68. }}
  69. />
  70. </Tabs>
  71. );
  72. }