react-native - 在 android 中 react native 开关设计

标签 react-native

是否有可能像 IOS 一样在 android 中获得相同的 react native switch View 。

已经尝试了一些 NPM 包(toggle-switch-react-native、react-native-flip-toggle-button),但它们不适用于 typescript 。

最佳答案

如何为 typescript 创建自定义组件?

import * as React from 'react';
import { Animated, Easing, StyleSheet, Text, TouchableOpacity, View } from 'react-native';

interface Props {
  onColor: string,
  offColor: string,
  label: string,
  onToggle: () => void,
  style: object,
  isOn: boolean,
  labelStyle: object
}

interface DefaultProps {
  onColor: string,
  offColor: string,
  label: string,
  onToggle: () => void,
  style: object,
  isOn: boolean,
  labelStyle: object
}

export default class Toggle extends React.PureComponent<Props> {

  animatedValue = new Animated.Value(0);

  static defaultProps: DefaultProps = {
    onColor: '#4cd137',
    offColor: '#ecf0f1',
    label: '',
    onToggle: () => { },
    style: {},
    isOn: false,
    labelStyle: {}
  }

  render() {

    const moveToggle = this.animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 20],
    });

    const {
      isOn,
      onColor,
      offColor,
      style,
      onToggle,
      labelStyle,
      label
    } = this.props;

    const color = isOn ? onColor : offColor;

    this.animatedValue.setValue(isOn ? 0 : 1);

    Animated.timing(this.animatedValue, {
      toValue: isOn ? 1 : 0,
      duration: 300,
      easing: Easing.linear,
    }).start();

    return (
      <View style={styles.container}>

        {!!label && <Text style={[styles.label, labelStyle]}>{label}</Text>}

        <TouchableOpacity onPress={() => {

          typeof onToggle === 'function' && onToggle();

        }}>
          <View
            style={[
              styles.toggleContainer,
              style,
              { backgroundColor: color }
            ]}>
            <Animated.View
              style={[
                styles.toggleWheelStyle, {
                  marginLeft: moveToggle,
                }]}
            />
          </View>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center'
  },
  toggleContainer: {
    width: 50,
    height: 30,
    marginLeft: 3,
    borderRadius: 15,
    justifyContent: 'center',
  },
  label: {
    marginRight: 2,
  },
  toggleWheelStyle: {
    width: 25,
    height: 25,
    backgroundColor: 'white',
    borderRadius: 12.5,
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 2
    },
    shadowOpacity: 0.2,
    shadowRadius: 2.5,
    elevation: 1.5,
  }
})

像这样使用

<View style={styles.container}>
  <Toggle
    isOn={this.state.isOn}
    style={{ marginBottom: 10 }}
    onToggle={this.onToggle}
  />
  <Toggle
    label={'With Label'}
    isOn={this.state.more}
    onToggle={this.onToggleMore}
  />
</View>

DEMO

关于react-native - 在 android 中 react native 开关设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462368/

相关文章:

react-native - 是否有禁用选项卡按钮的选项

ios - ld : symbol(s) not found for architecture x86_64 (realm)

javascript - react native : Alert message doesn't work when called frequently

react-native - 在 react 原生 ScrollView 中设置偏移限制

reactjs - 如何创建自定义下拉菜单 react native

react-native - 用于在 Expo/CNApp 项目上设置 Redux 的样板代码?

javascript - 为什么 react-native-onboarding-swiper 不能在我的启动画面上工作?

react-native - 在 IOS 中运行 React Native 应用程序时出错

ios - React Native 指纹采集

android - 循环一个 React-Native UI 组件