简体   繁体   English

如何创建简单的开关切换以在 React Native 中的深色主题和浅色主题之间切换

[英]How to create simple switch toggle to switch between dark theme and light theme in react native

I am creating a simple switch component that is if turned on the theme of the app will be light otherwise it would be dark.我正在创建一个简单的开关组件,如果打开该应用程序的主题,则它会变亮,否则会变暗。

Here is what i tried but it is not working.这是我尝试过的方法,但没有用。 How can i make the toggle switch the theme of the whole app?如何使切换开关成为整个应用程序的主题?

import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View, Switch, Appearance } from 'react-native';

export default function App() {
  const [isEnabled, setIsEnabled] = useState(false);

  const toggleSwitch = () => setIsEnabled(previousState => !previousState);
  const colorScheme = Appearance.getColorScheme();
  if (colorScheme === 'dark') {
    
  }
  return (
    <View style={styles.container}>
      <StatusBar style="auto" />
      <Switch
        style={{marginBottom:500, marginLeft:150}}
        trackColor={{ false: "light", true: "dark" }}
        thumbColor={isEnabled ? colorScheme == 'light' : colorScheme == 'dark'}
        ios_backgroundColor="#3e3e3e"
        onValueChange={toggleSwitch}
        value={isEnabled}
      />
    </View>
  );

} }

You can simply use useEffect here.您可以在这里简单地使用 useEffect 。

        import { StatusBar } from 'expo-status-bar';
        import React, {useState,useEffect} from 'react';
        import { StyleSheet, Text, View, Switch, Appearance } from 'react-native';
        
        export default function App() {
          const [isEnabled, setIsEnabled] = useState(false);
        
          const toggleSwitch = () => setIsEnabled(previousState => !previousState);
    
          // add this
          useEffect(()=>{
            const colorScheme = Appearance.getColorScheme();
              if (colorScheme === 'dark') {
                 setIsEnabled(true); // true means dark
              }else{
                 setIsEnabled(false); // false means light
              }
          },[])
    
          return (
            <View style={styles.container}>
              <StatusBar style="auto" />
              <Switch
                style={{marginBottom:500, marginLeft:150}}
                trackColor={{ false: "light", true: "dark" }}
                thumbColor={isEnabled ? colorScheme == 'light' : colorScheme == 'dark'}
                ios_backgroundColor="#3e3e3e"
                onValueChange={toggleSwitch}
                value={isEnabled}
              />
            </View>
          );
        }

Try usingreact-native-appearance-control .尝试使用react-native-appearance-control The Appearance API in react-native is read-only, but react-native-appearance-control provides methods for changing the underlying Color Scheme value for your app. react-native 中的 Appearance API 是只读的,但react-native-appearance-control提供了更改应用程序底层配色方案值的方法。

eg例如

import React, {useState, useEffect} from 'react';
import { Text, View, Switch, Appearance } from 'react-native';
import { setAppearanceLight, setAppearanceDark } from 'react-native-appearance-control';
        
export default function App() {
  const [isEnabled, setIsEnabled] = useState(Appearance.getColorScheme() === 'dark');       
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);
    
  useEffect(()=>{
    if (isEnabled) {
      setAppearanceDark();
    } else {
      setAppearanceLight();
    }
  }, [isEnabled])

  return (
    <View>
      ...
      <Switch
        ...
        onValueChange={toggleSwitch}
        value={isEnabled}
      />
    </View>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非材质ui组件中明暗主题切换 - Switch between dark and light theme in non-material ui component 如何使用 React 在 Bootstrap 5 中创建深色/浅色主题 - How to create dark/light theme in Bootstrap 5 with React 标题中的 MUI 主题亮/暗开关(AppBar) - MUI theme light/dark switch in header(AppBar) 如何强制更新我的反应代码中的组件以将深色主题切换为浅色主题 - How do I force Update my components in my react code to toggle dark theme to light theme 如何使用 Switch 和 withStyles() 动态更改主题的调色板类型(暗/亮) - How to dynamically change palette type (dark/light) of theme using Switch and withStyles() 当当前主题存储在本地时,如何在浅色和深色主题之间切换? - How do you toggle between light and dark themes while current theme is being locally stored? 如何在使用 header 作为开关位置的同时将我的亮/暗模式主题包裹在 index.js 周围? - How can I wrap my light/dark mode theme around index.js while using my header as the switch location? React:antd 在运行时更改深色或浅色主题 - React: antd change dark or light theme on runtime 我有一个正在尝试改进的浅色/深色主题切换反应组件 - I have a light/dark theme toggle react component that I'm trying to improve 应用程序刷新时,用 redux 反应本机主题开关消失 - react native theme switch with redux disappear when app refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM