react-native-popup-dialog实现通用加载层

迷南。 2022-03-25 10:07 249阅读 0赞

2019-01-20

加载层组件效果如图:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNzIxMzgy_size_16_color_FFFFFF_t_70

react-native-popup-dialog是一个很不错的对话框库,我借用它的遮盖层实现了加载层效果。

地址:https://github.com/jacklam718/react-native-popup-dialog/blob/master/README.md

使用demo代码:

  1. import React from 'react';
  2. import {
  3. Button,
  4. View,
  5. } from 'react-native';
  6. import {
  7. LoadingDialog
  8. } from './../../utils/LoadingDialogUtils';
  9. /**
  10. * dialog-component分支
  11. */
  12. export default class TestLoadingDialogUtilsExample extends React.Component {
  13. static navigationOptions = {
  14. headerTitle: '测试加载对话框工具类',
  15. };
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. defaultAnimationDialog: false,
  20. loadingDialogVisible: false, //是否显示加载层
  21. loadingHintText: null, //加载层提示语
  22. }
  23. }
  24. /**
  25. * 打开加载对话框
  26. * @param loadingHintText 提示文本
  27. */
  28. showLoadingDialog = (loadingHintText) => {
  29. this.setState({
  30. loadingDialogVisible: true,
  31. loadingHintText: loadingHintText,
  32. });
  33. };
  34. /**
  35. * 关闭加载对话框
  36. */
  37. closeLoadingDialog = () => {
  38. this.setState({
  39. loadingDialogVisible: false,
  40. loadingHintText: null,
  41. });
  42. };
  43. render() {
  44. return (
  45. <View style={
  46. {
  47. flex: 1,
  48. justifyContent: 'center',
  49. alignItems: 'center'
  50. }}>
  51. <Button title="打开loadingDialog" onPress={() => {
  52. this.showLoadingDialog('加载中...');
  53. setTimeout(() => {
  54. this.closeLoadingDialog();
  55. }, 3000);
  56. }}/>
  57. <LoadingDialog
  58. loadingDialogVisible={this.state.loadingDialogVisible}
  59. loadingHintText={this.state.loadingHintText}
  60. />
  61. </View>
  62. )
  63. }
  64. }

工具类代码:

  1. import React from 'react';
  2. import {ActivityIndicator, Text, View} from "react-native";
  3. import Dialog from "react-native-popup-dialog";
  4. const GlobalLoadingDialogStyle = {
  5. //Dialog的宽度:屏幕的百分比
  6. loadingDialogWidthPercent: 0.35,
  7. //Dialog的高度
  8. loadingDialogHeightPercent: 0.15,
  9. //设置遮盖层的背景色
  10. overlayBackgroundColor: '#00000000',
  11. //Dialog样式
  12. dialogStyle: {
  13. backgroundColor: 'rgba(0,0,0,0.8)',
  14. },
  15. //Dialog内容容器的样式
  16. dialogContentContainerStyle: {
  17. flex: 1,
  18. justifyContent: 'center',
  19. alignItems: 'center',
  20. },
  21. //提示文字的样式
  22. loadingHintTextStyle: {
  23. fontSize: 16,
  24. color: '#fff',
  25. marginTop: 10
  26. }
  27. };
  28. /**
  29. * 2018-12-14
  30. * chenlw
  31. * work:封装Loading对话框代码
  32. * @param loadingDialogVisible 对话框是否显示
  33. * @param loadingHintText 加载提示文本
  34. * @returns {*}
  35. * @constructor
  36. */
  37. function LoadingDialog({loadingDialogVisible, loadingHintText}) {
  38. return (
  39. <Dialog
  40. width={GlobalLoadingDialogStyle.loadingDialogWidthPercent}
  41. height={GlobalLoadingDialogStyle.loadingDialogHeightPercent}
  42. visible={loadingDialogVisible}
  43. rounded
  44. overlayBackgroundColor={GlobalLoadingDialogStyle.overlayBackgroundColor}
  45. dialogStyle={GlobalLoadingDialogStyle.dialogStyle}
  46. >
  47. <View style={GlobalLoadingDialogStyle.dialogContentContainerStyle}>
  48. <ActivityIndicator size="large"/>
  49. <Text
  50. style={GlobalLoadingDialogStyle.loadingHintTextStyle}>{loadingHintText}</Text>
  51. </View>
  52. </Dialog>
  53. );
  54. }
  55. export {LoadingDialog};

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读