天天看點

react-native Toast簡單封裝

import {Component} from 'react';
import Toast from 'react-native-tiny-toast';
import {setSpText} from './util';

export default class ToastBox extends Component {
  static show = content => {
    // toastShort toastLong
    if (this.toast !== undefined) {
      Toast.hide(this.toast);
    }
    this.toast = Toast.show(content.toString(), {
      duration: 2000,
      position: 0,
      shadow: true,
      animation: true,
      hideOnPress: true,
      delay: 0,
      textStyle: {fontSize: setSpText(28)}
    });
  };

  static showTop = content => {
    // toastShortTop
    if (this.toast !== undefined) {
      Toast.hide(this.toast);
    }
    this.toast = Toast.show(content.toString(), {
      duration: 2000,
      position: 100,
      shadow: true,
      animation: false,
      hideOnPress: true,
      delay: 0,
      textStyle: {fontSize: setSpText(28)}
    });
  };

  static showBottom = content => {
    // toastShortBottom
    if (this.toast !== undefined) {
      Toast.hide(this.toast);
    }
    this.toast = Toast.show(content.toString(), {
      duration: 2000,
      position: -200,
      shadow: true,
      animation: true,
      hideOnPress: true,
      delay: 0,
      textStyle: {fontSize: setSpText(28)}
    });
  };

  static loading = () => {
    if (this.toast !== undefined) {
      Toast.hide(this.toast);
    }
    this.toast = Toast.showLoading();
  };

  static hide = () => {
    if (this.toast !== undefined) {
      Toast.hide(this.toast);
    }
    this.toast = Toast.hide();
  };
}
           

繼續閱讀