-1

Are there some ways in react-native to create a switcher with this exact design? enter image description here

I'm using this React Native Switch, it works perfect, but I need plus text 'on' and 'off', maybe custom component? https://reactnative.dev/docs/switch

Thank you!

Manche
  • 47
  • 2
  • 10
  • https://github.com/arshigtx/react-native-custom-switch seems good, or source code can give you idea how to make a simple one. – linusw Sep 05 '22 at 11:02
  • I need to use react-native components and don’t use any other libraries – Manche Sep 05 '22 at 11:10
  • Have you tried anything yet? – David Scholz Sep 05 '22 at 11:11
  • @user16599051 you didn't read the second half of my comment I guess. This library doesn't have any dependency other than react and react-native. You can literally just copy the source code and use as your own component. – linusw Sep 05 '22 at 11:15
  • export const ToggleButton = () => { const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState); return ( ); }; I don't know how ti put text off and on – Manche Sep 05 '22 at 11:15

1 Answers1

1

You could create your own custom component with 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. 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

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;
  • I build my project with that code, it's successful, but nothing show on browser – Manche Sep 05 '22 at 11:34
  • Can you play with styles maybe you missed flex:1 if you can share any code we can look in to it – Cafer Yükseloğlu Sep 05 '22 at 11:37
  • https://reactnative.dev/docs/switch this is working perfectly, only I don't know how to add text – Manche Sep 05 '22 at 11:37
  • I use exactly the some code, only changes I made is colors, I need only to add "off" and "on" export const ToggleButton = () => { const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState); return ( ); }; – Manche Sep 05 '22 at 11:40
  • https://www.youtube.com/watch?v=yXdRDyvvlvQ&ab_channel=Indently I've tried with this but didn't working – Manche Sep 05 '22 at 12:22
  • Edited my answer check again – Cafer Yükseloğlu Sep 05 '22 at 13:30