|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, Text, StatusBar, Button, StyleSheet } from 'react-native'; |
| 3 | +import { useIsFocused } from '@react-navigation/native'; |
| 4 | +import { NavigationContainer } from '@react-navigation/native'; |
| 5 | +import { createStackNavigator } from '@react-navigation/stack'; |
| 6 | +import { |
| 7 | + SafeAreaProvider, |
| 8 | + useSafeAreaInsets, |
| 9 | +} from 'react-native-safe-area-context'; |
| 10 | + |
| 11 | +function FocusAwareStatusBar(props) { |
| 12 | + const isFocused = useIsFocused(); |
| 13 | + |
| 14 | + return isFocused ? <StatusBar {...props} /> : null; |
| 15 | +} |
| 16 | + |
| 17 | +function Screen1({ navigation }) { |
| 18 | + const insets = useSafeAreaInsets(); |
| 19 | + |
| 20 | + return ( |
| 21 | + <View |
| 22 | + style={[ |
| 23 | + styles.container, |
| 24 | + { |
| 25 | + backgroundColor: '#6a51ae', |
| 26 | + paddingTop: insets.top, |
| 27 | + paddingBottom: insets.bottom, |
| 28 | + paddingLeft: insets.left, |
| 29 | + paddingRight: insets.right, |
| 30 | + }, |
| 31 | + ]} |
| 32 | + > |
| 33 | + <FocusAwareStatusBar barStyle="light-content" backgroundColor="#6a51ae" /> |
| 34 | + <Text style={{ color: '#fff' }}>Light Screen</Text> |
| 35 | + <Button |
| 36 | + title="Next screen" |
| 37 | + onPress={() => navigation.navigate('Screen2')} |
| 38 | + color="#fff" |
| 39 | + /> |
| 40 | + </View> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function Screen2({ navigation }) { |
| 45 | + const insets = useSafeAreaInsets(); |
| 46 | + |
| 47 | + return ( |
| 48 | + <View |
| 49 | + style={[ |
| 50 | + styles.container, |
| 51 | + { |
| 52 | + backgroundColor: '#ecf0f1', |
| 53 | + paddingTop: insets.top, |
| 54 | + paddingBottom: insets.bottom, |
| 55 | + paddingLeft: insets.left, |
| 56 | + paddingRight: insets.right, |
| 57 | + }, |
| 58 | + ]} |
| 59 | + > |
| 60 | + <FocusAwareStatusBar barStyle="dark-content" backgroundColor="#ecf0f1" /> |
| 61 | + <Text>Dark Screen</Text> |
| 62 | + <Button |
| 63 | + title="Next screen" |
| 64 | + onPress={() => navigation.navigate('Screen1')} |
| 65 | + /> |
| 66 | + </View> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +const Stack = createStackNavigator(); |
| 71 | + |
| 72 | +export default function App() { |
| 73 | + return ( |
| 74 | + <SafeAreaProvider> |
| 75 | + <NavigationContainer> |
| 76 | + <Stack.Navigator screenOptions={{ headerShown: false }}> |
| 77 | + <Stack.Screen name="Screen1" component={Screen1} /> |
| 78 | + <Stack.Screen name="Screen2" component={Screen2} /> |
| 79 | + </Stack.Navigator> |
| 80 | + </NavigationContainer> |
| 81 | + </SafeAreaProvider> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +const styles = StyleSheet.create({ |
| 86 | + container: { |
| 87 | + flex: 1, |
| 88 | + justifyContent: 'center', |
| 89 | + alignItems: 'center', |
| 90 | + }, |
| 91 | +}); |
0 commit comments