import { useColorScheme } from "react-native";
import { Colors } from "@/constants/Colors";
export function useThemeColor(
props: { light?: string; dark?: string },
colorName: keyof typeof Colors.light & keyof typeof Colors.dark
) {
const theme = useColorScheme() ?? "light";
const colorFromProps = props[theme];
if (colorFromProps) {
return colorFromProps;
} else {
return Colors[theme][colorName];
}
}
这是我使用的自定义组件:
import { View, type ViewProps } from "react-native";
import { useThemeColor } from "@/hooks/useThemeColor";
export type ThemedViewProps = ViewProps & {
lightColor?: string;
darkColor?: string;
};
export function ThemedView({
style,
lightColor,
darkColor,
...otherProps
}: ThemedViewProps) {
const backgroundColor = useThemeColor(
{ light: lightColor, dark: darkColor },
"background"
);
return <View style={[{ backgroundColor }, style]} {...otherProps} />;
}
I查看了此平台上的各种帖子,但找不到实际使用React-Navigation/atnation的theprovider的文章。
import {Appearance} from 'react-native';
黑暗主题
Appearance.setColorScheme('dark');
为Light主题
Appearance.setColorScheme('light');
反射本地外观