router.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React from 'react';
  2. import {NavigationContainer} from '@react-navigation/native';
  3. import {Platform, Text, BackHandler, ToastAndroid, StyleSheet} from 'react-native';
  4. import {connect} from 'react-redux';
  5. import {createNativeStackNavigator} from '@react-navigation/native-stack';
  6. // 忽略不必要的警告日志
  7. import {LogBox} from 'react-native';
  8. LogBox.ignoreLogs([
  9. 'Non-serializable values were found in the navigation state',
  10. 'componentWillReceiveProps',
  11. 'EventEmitter.removeListener',
  12. ]);
  13. import Welcome from './pages/Welcome';
  14. import HomeStack from './pages/Desktop/Index';
  15. const Stack = createNativeStackNavigator();
  16. let lastBackPressed = 0;
  17. const NavigationOptions = {
  18. headerStyle: {
  19. backgroundColor: '#5394e4',
  20. height: 40,
  21. },
  22. headerShadowVisible: false, // 取消导航下面的阴影
  23. headerTitleAlign: 'center', // 标题文字居中
  24. headerTintColor: '#fff',
  25. headerTitleStyle: {
  26. fontSize: 20,
  27. marginTop: Platform.OS === 'ios' ? 10 : 0,
  28. },
  29. headerBackTitle: Platform.OS === 'ios' ? ' ' : null, // ~注意~ 这个地方是隐藏返回按钮文字的
  30. };
  31. @connect(({app, router}) => ({app, router}))
  32. class Router extends React.PureComponent {
  33. // componentDidMount() {
  34. // BackHandler.addEventListener('hardwareBackPress', this.backAction);
  35. // }
  36. //
  37. // componentWillUnmount() {
  38. // BackHandler.removeEventListener('hardwareBackPress', this.backAction);
  39. // }
  40. //
  41. // backAction = () => {
  42. // const currentScreen = getActiveRouteName(this.props.navigation.getState());
  43. // const MainRouteNames = ['DesktopHome'];
  44. // if (MainRouteNames.indexOf(currentScreen) > -1) {
  45. // if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) {
  46. // //最近2秒内按过back键,可以退出应用。
  47. // BackHandler.exitApp();
  48. // return false;
  49. // }
  50. // lastBackPressed = Date.now();
  51. // ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
  52. // return true;
  53. // }
  54. // };
  55. render() {
  56. const StackDict = {
  57. ...HomeStack,
  58. };
  59. let stackList = [];
  60. for (let key in StackDict) {
  61. const item = {
  62. name: key,
  63. screen: StackDict[key].screen,
  64. options: {
  65. ...NavigationOptions, ...StackDict[key].navigationOptions, ...StackDict[key].screen.navigationOptions,
  66. },
  67. };
  68. stackList.push(item);
  69. }
  70. return (
  71. <NavigationContainer>
  72. <Stack.Navigator>
  73. {/*<Stack.Screen name="Welcome" component={Welcome} options={{headerShown: false}}/>*/}
  74. {/*<Stack.Screen name="DesktopHome" component={DesktopHome} options={NavigationOptions}/>*/}
  75. {/*<Stack.Screen name="SetPower" component={SetPower} options={{headerShown: true}}/>*/}
  76. {
  77. stackList.map((stack, index) => (
  78. <Stack.Screen key={index} name={stack.name} component={stack.screen}
  79. options={stack.options}/>
  80. ))
  81. }
  82. </Stack.Navigator>
  83. </NavigationContainer>
  84. );
  85. }
  86. }
  87. const styles = StyleSheet.create({
  88. iconFont: {
  89. fontFamily: 'iconfont',
  90. fontSize: 20,
  91. color: '#757575',
  92. },
  93. iconFontFocused: {
  94. fontFamily: 'iconfont',
  95. fontSize: 20,
  96. color: '#2b90ea',
  97. },
  98. });
  99. export default Router;