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
Add a ment  | 

3 Answers 3

Reset to default 3

Applying 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