_layout.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. console.log(segment);
  14. // Calculate marginBottom based on screen height
  15. const calculateMarginBottom = () => {
  16. if (Platform.OS === 'ios') return 0;
  17. if (height < 600) return 0;
  18. return 15;
  19. };
  20. useEffect(() => {
  21. // Determine if the tab bar should be visible based on the current segment
  22. const isScanQrPage = segment[segment.length - 1] === 'scanQrPage';
  23. setTabBarVisible(!isScanQrPage);
  24. }, [segment]);
  25. return (
  26. <Tabs
  27. screenOptions={{
  28. headerShown: false,
  29. tabBarLabelStyle: {
  30. fontSize: 16,
  31. marginBottom: calculateMarginBottom(),
  32. color: colorScheme === 'dark' ? '#CCCCCC' : '#666666'
  33. },
  34. tabBarStyle: {
  35. height: height * 0.105,
  36. display: tabBarVisible ? 'flex' : 'none',
  37. backgroundColor: colorScheme === 'dark' ? '#041316' : '#FFFFFF'
  38. },
  39. tabBarIconStyle: {
  40. width: 10,
  41. height: 10,
  42. marginTop: 15,
  43. marginBottom: 15,
  44. }
  45. }}
  46. >
  47. <Tabs.Screen
  48. name="(home)"
  49. options={{
  50. title: '主頁',
  51. // tabBarHideOnKeyboard: true,
  52. tabBarIcon: ({ focused }) => <TabHomeIcon color={focused ? activeColor : inactiveColor} />
  53. }}
  54. />
  55. <Tabs.Screen
  56. name="(charging)"
  57. options={{
  58. title: '充電',
  59. tabBarHideOnKeyboard: true,
  60. tabBarIcon: ({ focused }) => <TabChargingIcon color={focused ? activeColor : inactiveColor} />
  61. }}
  62. />
  63. <Tabs.Screen
  64. name="(account)"
  65. options={{
  66. title: '帳戶',
  67. tabBarHideOnKeyboard: true,
  68. tabBarIcon: ({ focused }) => <TabAccountIcon color={focused ? activeColor : inactiveColor} />
  69. }}
  70. />
  71. </Tabs>
  72. );
  73. }