admin管理员组文章数量:1290144
How do I set the theme to dark theme in React Native Paper? In all my screens, all the <View>
's still have white backgrounds.
const theme = {
...DarkTheme,
colors: {
...DarkTheme.colors,
primary: '#2d3436',
accent: '#1C1C1C',
background : '#636e72'
}
};
render() {
return(
<PaperProvider theme={theme}>
<App />
</PaperProvider>
);
}
How do I set the theme to dark theme in React Native Paper? In all my screens, all the <View>
's still have white backgrounds.
const theme = {
...DarkTheme,
colors: {
...DarkTheme.colors,
primary: '#2d3436',
accent: '#1C1C1C',
background : '#636e72'
}
};
render() {
return(
<PaperProvider theme={theme}>
<App />
</PaperProvider>
);
}
Share
Improve this question
edited Feb 7, 2021 at 18:25
benomatis
5,6437 gold badges39 silver badges60 bronze badges
asked Aug 12, 2020 at 0:02
SamSam
30.3k75 gold badges252 silver badges462 bronze badges
3 Answers
Reset to default 3Applying a theme and provider level wont change all the views. You will have to use the 'withTheme' when exporting which would provide the theme prop which you can use to access the colors.
import { withTheme } from 'react-native-paper';
const Test = ({ theme,children }) => {
const { colors } = theme;
return (
<View style={{ backgroundColor: colors.background }}>
{children}
</View>
);
};
export default withTheme(Test);
If you want to use the same theme for all Views, create a custom wrapper ponent which sets the color like above
Instead of adding extra hooks on every screen/ponent you can configure the theme with react-navigation.
https://callstack.github.io/react-native-paper/docs/guides/theming-with-react-navigation#passing-theme-with-providers
You have to set the theme at <NavigationContainer theme={navigationTheme}>{your code}</NavigationContainer>
.
The navigationTheme
is of the type Theme
, but react native paper theme is of the type MD3Theme
, so you will have to addapt your MD3Theme
to the type Theme
.
I have made it like this
const navigationTheme = {
dark: colorScheme === "dark",
colors: {
border: theme.colors.background,
background: theme.colors.background,
card: null,
notification: theme.colors.outline,
primary: theme.colors.primary,
text: theme.colors.outline,
},
};
where theme
is my preconfigured MD3Theme
.
本文标签: javascriptReact Native Paper Dark ThemeStack Overflow
版权声明:本文标题:javascript - React Native Paper Dark Theme - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741492969a2381706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论