【问题标题】:How to change theme color throught the Toggle in React Native?如何通过 React Native 中的 Toggle 更改主题颜色?
【发布时间】:2019-08-20 23:05:02
【问题描述】:

我想更改应用程序的theme 颜色。就像我现在的主题很轻,但我想通过帮助切换按钮来更改暗模式之类的主题。 我的应用程序正在做一些工作。链接:https://www.howtogeek.com/361407/how-to-enable-dark-mode-for-youtube/

我构建了这个应用程序,但在全球范围内不工作,它只能在当前页面工作,就像在设置页面工作一样,但不能在主页或个人资料页面工作

我没有源代码,但我使用这种类型https://www.seishin.me/dynamic-switching-of-themes-in-react-native-app/

只有一页工作,但在全局工作,就像在设置中工作一样,因为我在设置页面中编写代码,但在个人资料页面或主页中不工作。

我累了…………

【问题讨论】:

  • 您想一次更改所有屏幕的主题吗?
  • 是的,但是我改变了唯一的颜色,比如背景颜色和字体颜色。
  • 你可以使用 redux 来处理这个问题。获取 2 组常量文件,您将在其中拥有所有颜色,并在需要时在每个类中导入这两个文件,同时使用道具设置颜色检查。例如:Hello 当你异步更改主题商店时,你改变了主题,所以当你关闭应用程序并再次打开时,你可以在初始屏幕中应用什么主题并且可以相应地更新你的道具。如果需要更多信息,请告诉我。

标签: reactjs react-native


【解决方案1】:

我创建了一个按钮来更改所有屏幕的背景颜色。这是你想要的吗?

Example link由我创建

import React,{Component} from 'react';
import { Button, Text, View } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import { AppContextProvider,AppConsumer } from './AppContextProvider'
import { BlueGray, LightGreen } from './Themes'

export default class App extends Component {
  render() {
    return ( <AppContextProvider>
                <MyNavigator />
            </AppContextProvider>);
  }
}


class ScreenComponentOne extends React.Component {
  static navigationOptions = {
    headerTitle: 'First screen',
  };

  render() {
    return (
      <AppConsumer>
          { appConsumer => (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          backgroundColor: appConsumer.theme.colors.primary
        }}>
        <Button
          title="Go to two"
          onPress={() => this.props.navigation.navigate('RouteNameTwo')}
        />
        <Button onPress={ () => appConsumer.updateTheme(BlueGray) } title="Blue Gray Theme"></Button>
        <Button onPress={ () => appConsumer.updateTheme(LightGreen) } title="Light Green Theme"></Button>
      </View>
                      )}
            </AppConsumer>
    );
  }
}

class ScreenComponentTwo extends React.Component {
  static navigationOptions = {
    headerTitle: 'Second screen',
  };

  render() {
    return (
            <AppConsumer>
          { appConsumer => (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          backgroundColor: appConsumer.theme.colors.primary
        }}>
        <Button
          title="Go to three"
          onPress={() =>
            this.props.navigation.navigate('RouteNameThree')
          }
        />
         <Button
          title="Go back"
          onPress={() => this.props.navigation.goBack()}
        />
      </View>
                            )}
            </AppConsumer>
    );
  }
}

class ScreenComponentThree extends React.Component {
  static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: `Number: ${navigation.getParam('randomNumber')}`,
    };
  };

  render() {
    return (
      <AppConsumer>
          { appConsumer => (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: appConsumer.theme.colors.primary
        }}>
        <Button
          title="Add another two"
          onPress={() => this.props.navigation.push('RouteNameTwo')}
        />
        <Button
          title="Go back"
          onPress={() => this.props.navigation.goBack()}
        />
      </View>
                            )}
            </AppConsumer>
    );
  }
}

const MyNavigator = createStackNavigator(
  {
    RouteNameOne: ScreenComponentOne,
    RouteNameTwo: ScreenComponentTwo,
    RouteNameThree: ScreenComponentThree,
  },
  {
    // headerTransitionPreset: 'uikit',
    // mode: 'modal',
  }
);

AppContextProvider.js

import React, { Component } from "react";
import { BlueGray, LightGreen } from './Themes'

const Context = React.createContext();

export class AppContextProvider extends Component {
    state = {
        theme: LightGreen,
        updateTheme: (theme) => {
            this.setState({ theme: theme })
        }
    }

    render() {
        const { theme } = this.state
        return (
            <Context.Provider value={ this.state }  theme={ theme } >
                    { this.props.children }
            </Context.Provider>
        )
    }
}

export const AppConsumer = Context.Consumer;
export const AppContext = Context;

Themes.js

import { DefaultTheme } from "react-native-paper";

export const BlueGray = {
    ...DefaultTheme,
    colors: {
        ...DefaultTheme.colors,
        primary: '#607d8b'
    }
}

export const LightGreen = {
    ...DefaultTheme,
    colors: {
        ...DefaultTheme.colors,
        primary: '#8bc34a'
    }
}

【讨论】:

  • 如果我不使用react-native-paper我该如何处理?
猜你喜欢
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多