I would like to add one of these slider toggle buttons to my react native app. Are there any easy ways to do this without starting from scratch?
Asked
Active
Viewed 2,359 times
0
-
Please share your code snippet instead of video – Taylor Rahul Jan 12 '21 at 08:40
-
I don’t have a code snippet, that’s the point. I want to know if there’s a way to do this. – RiddleRiddlerRddler Jan 12 '21 at 09:12
3 Answers
7

Quang Thái
- 649
- 5
- 17
-
-
This is exactly what I was after, thank you. I made it a bit smaller and added some drop shadow and it looks perfect, thanks! My snack: https://snack.expo.io/@marccashmore/slide-button – RiddleRiddlerRddler Jan 12 '21 at 11:33
3
This is a basic example of how you can achieve this using Animated
API.
import React from 'react';
import {
Animated,
Easing,
StyleSheet,
Pressable,
View,
Text,
} from 'react-native';
const App = () => {
const animatedValue = React.useRef(new Animated.Value(0)).current;
const startAnimation = (toValue) => {
Animated.timing(animatedValue, {
toValue,
duration: 400,
easing: Easing.linear,
useNativeDriver: false,
}).start();
};
const left = animatedValue.interpolate({
inputRange: [0, 1],
outputRange: ['2%', '50%'],
extrapolate: 'clamp',
});
const scale = animatedValue.interpolate({
inputRange: [0, 0.5, 1],
outputRange: [1, 0.9, 1],
extrapolate: 'clamp',
});
return (
<View style={styles.container}>
<View style={styles.sliderContainer}>
<Animated.View style={[styles.slider, { left }]} />
<Pressable
style={styles.clickableArea}
onPress={startAnimation.bind(null, 0)}>
<Animated.Text
style={[styles.sliderText, { transform: [{ scale }] }]}>
Active
</Animated.Text>
</Pressable>
<Pressable
style={styles.clickableArea}
onPress={startAnimation.bind(null, 1)}>
<Animated.Text
style={[styles.sliderText, { transform: [{ scale }] }]}>
History
</Animated.Text>
</Pressable>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
sliderContainer: {
width: '90%',
height: 50,
borderRadius: 10,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: '#e0e0e0',
},
clickableArea: {
width: '50%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
},
sliderText: {
fontSize: 17,
fontWeight: '500',
},
slider: {
position: 'absolute',
width: '48%',
height: '90%',
borderRadius: 10,
backgroundColor: '#f4f4f4',
},
});
export default App;
Here you can check out the demo of how it works.

Leri Gogsadze
- 2,958
- 2
- 15
- 24
0
use native base tabs and then customize them .
this link will help you
https://docs.nativebase.io/Components.html#tabs-def-headref

Bisma Azher
- 689
- 8
- 9