简体   繁体   English

反应本机开关

[英]React Native Switch

Are there some ways in react-native to create a switcher with this exact design? react-native 中是否有一些方法可以创建具有这种精确设计的切换器? 在此处输入图像描述

I'm using this React Native Switch, it works perfect, but I need plus text 'on' and 'off', maybe custom component?我正在使用这个 React Native Switch,它运行良好,但我需要加上文本“on”和“off”,也许是自定义组件? https://reactnative.dev/docs/switch https://reactnative.dev/docs/switch

Thank you!谢谢!

You could create your own custom component with React Native.你可以使用 React Native 创建自己的自定义组件。 Just Inherit it and Create Own component like as https://github.com/arshigtx/react-native-custom-switch create a component copy inner src fix issues then make it your own.只需继承它并创建自己的组件,例如https://github.com/arshigtx/react-native-custom-switch创建组件副本内部 src 修复问题,然后将其设为您自己的。 Please start the project.请启动项目。

----- Edited ------ ----- 已编辑 ------

https://snack-web-player.s3.us-west-1.amazonaws.com/v2/46/index.html?initialUrl=exp%3A%2F%2Fexp.host%2F%40snack%2Fsdk.46.0.0-HVS884TWR6&origin=https%3A%2F%2Fsnack.expo.dev&verbose=false https://snack-web-player.s3.us-west-1.amazonaws.com/v2/46/index.html?initialUrl=exp%3A%2F%2Fexp.host%2F%400sd.nack%。 0-HVS884TWR6&origin=https%3A%2F%2Fsnack.expo.dev&verbose=false

import React, { useState } from "react";
import { View, Switch, StyleSheet,Text } from "react-native";

const App = () => {
  const [isEnabled, setIsEnabled] = useState(false);
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);

  return (
    <View style={styles.container}>
      <View>
      {
        isEnabled ? <Text style={{position: 'absolute', color: 'white', top: 3, left: 2, zIndex: 5, fontSize: 11}} >On</Text> : <Text style={{position: 'absolute', color: 'white', top: 3, left: 20, zIndex: 5, fontSize: 11}}>Off</Text>
      }
      <Switch
        trackColor={{ false: "#767577", true: "#81b0ff" }}
        thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
        ios_backgroundColor="#3e3e3e"
        onValueChange={toggleSwitch}
        value={isEnabled}
      />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
  }
});

export default App;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM