👉文档
渐变色的按钮

安装

  1. yarn add react-native-linear-gradient

封装组件

  1. import {Text, StyleSheet, TouchableOpacity} from 'react-native';
  2. import React, {Component} from 'react';
  3. import LinearGradient from 'react-native-linear-gradient';
  4. import {px2dp} from '~/utils/screenKits';
  5. export default class LinearBtn extends Component {
  6. // props默认值
  7. static defaultProps = {
  8. style: {},
  9. textStyle: {},
  10. };
  11. render() {
  12. console.log(this.props);
  13. return (
  14. <TouchableOpacity
  15. onPress={this.props.onPress}
  16. style={{
  17. width: '100%',
  18. height: '100%',
  19. ...this.props.style,
  20. overflow: 'hidden',
  21. }}>
  22. <LinearGradient
  23. start={{x: 0, y: 0}}
  24. end={{x: 1, y: 0}}
  25. colors={['#9b63cd', '#e0708c']}
  26. style={styles.linearGradient}>
  27. <Text style={{...styles.buttonText, ...this.props.textStyle}}>
  28. Sign in with Facebook
  29. </Text>
  30. </LinearGradient>
  31. </TouchableOpacity>
  32. );
  33. }
  34. }
  35. const styles = StyleSheet.create({
  36. linearGradient: {
  37. flex: 1,
  38. paddingLeft: px2dp(15),
  39. paddingRight: px2dp(15),
  40. borderRadius: px2dp(20),
  41. justifyContent: 'center',
  42. alignItems: 'center',
  43. },
  44. buttonText: {
  45. fontSize: px2dp(18),
  46. fontFamily: 'Gill Sans',
  47. // textAlign: 'center',
  48. color: '#ffffff',
  49. backgroundColor: 'transparent',
  50. },
  51. });

使用

  1. import LinearBtn from '~/components/LinearBtn';
  2. <LinearBtn style={{width: px2dp(300), height: px2dp(50)}} />

效果

image.png