我正在使用 React Hooks 时在我的应用程序中实现暗/亮模式,但我不确定如何为StackNavigator制作组件或将道具传递到StackNavigator。如何将颜色从我的主题传递到我的 StackNavigator 中?请你能帮我:)
App.js
<AppearanceProvider>
<SwitchNavigator />
</AppearanceProvider>
主题/Index.js
const palette = {
black: "#000000",
white: "#ffffff",
yellow: '#F1C40F',
blue: '#21D2FF',
red: '#F8564F',
darkGreen: "#27AE60",
gray: '#404040',
lightGray: "#cdcdcd",
darkGray: "#303030",
};
export const colors = {
buttonYellowyBg: palette.yellow,
buttonBlueBg: palette.blue,
buttonRedBg: palette.red,
buttonPrimaryBg: palette.darkGreen,
buttonPrimaryBorderColor: palette.darkGreen,
buttonPrimaryColor: palette.white,
buttonPrimaryShadowColor: palette.lightGray,
keyBoardAvoidingViewColor: palette.white,
navPrimaryColor: palette.white
}
export const themedColors = {
default: {
...colors,
},
light: {
...colors,
},
dark: {
...colors,
buttonPrimaryBg: palette.darkGray,
buttonPrimaryColor: palette.white,
buttonPrimaryBorderColor: palette.darkGray,
buttonPrimaryShadowColor: palette.darkGray,
buttonYellowyBg: palette.darkGray,
buttonBlueBg: palette.darkGray,
buttonRedBg: palette.darkGray,
buttonRedBg: palette.darkGray,
keyBoardAvoidingViewColor: palette.darkGrayB,
navPrimaryColor: palette.darkGrayB,
}
};
主题/Hooks.js
import { useColorScheme } from 'react-native-appearance'
import { themedColors } from '.'
export const useTheme = () => {
const theme = useColorScheme()
const colors = theme ? themedColors[theme] : themedColors.default
return {
colors,
theme,
}
}
这是我如何使用颜色的示例: Components / TextInput.js
import React from "react";
import { TextInput } from "react-native";
import { useTheme } from "../../theme/hooks";
const MyTextInput = ({
placeholder,
value,
onChangeText,
onSubmitEditing
}) => {
const { colors } = useTheme();
return (
<TextInput
style={{
color: colors.inputTextColor,
marginRight: "5%",
marginLeft: "5%",
padding: 10,
fontSize: 25,
borderColor: colors.inputTextColor,
borderBottomWidth: 1
}}
placeholder={placeholder}
value={value}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
/>
);
};
export default MyTextInput;
我该如何做类似的事情或将颜色道具传递给以下内容: StackNavigator.js
import * as React from "react";
import { TouchableOpacity, Alert, StyleSheet, Text } from "react-native";
import MainScreen from "../screens/Main";
import LoginScreen from "../screens/signup/Login";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
const authNavigator = createStackNavigator(
{
Main: {
screen: MainScreen,
navigationOptions: {
headerShown: false
}
},
Login: {
screen: LoginScreen,
navigationOptions: ({ navigation }) => ({
title: navigation.state.params.title,
headerTintColor: "#404040",
headerTitleStyle: {
color: "#404040",
fontSize: 25,
fontFamily: "gadugi"
},
headerBackTitleStyle: {
color: "#404040",
fontFamily: "roboto"
},
headerStyle: {
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
}
})
}
},
{
initialRouteName: "Main",
defaultNavigationOptions: {
headerBackTitle: null
}
}
);
export default createAppContainer(authNavigator);
答案 0 :(得分:0)
因此React-navigation v5的稳定版本最近问世了。
这使我可以像这样传递主题和颜色:
import { useTheme } from "../theme/hooks";
...
const Stack = createStackNavigator();
function Auth() {
const { colors } = useTheme();
return (
<Stack.Navigator
initialRouteName="Main"
screenOptions={{
headerBackTitle: " ",
headerTintColor: colors.navTextPrimaryColor,
headerTitleStyle: {
color: colors.navTextPrimaryColor,
fontSize: 25,
fontFamily: "gadugi"
},
headerBackTitleStyle: {
color: colors.navTextPrimaryColor,
fontFamily: "roboto"
},
headerStyle: {
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
backgroundColor: colors.navPrimaryColor
}
}}
>
<Stack.Screen
name="Main"
component={MainScreen}
options={{
headerShown: false
}}
/>
<Stack.Screen name="Login" component={LoginScreen} options={{title: ' '}}/>
</Stack.Navigator>
);
}
export default Auth;