RN中模态框的使用

1.安装依赖:

npm i react-native-modal --save

2.代码如下:

import React, { Component } from 'react';
import { 
    Text, 
    View,
    StyleSheet,
    TouchableOpacity,
} from 'react-native';

import Modal from 'react-native-modal';

export default class ModalIndex extends Component {
    constructor(props) {
        super(props);
        this.state={
            modalVisible: false,
        }
    }

    close = () => {
        this.setState({
            modalVisible: false,
        })
    }

    openModal = () => {
        this.setState({
            modalVisible: true,
        })
    }
    render() {
        return (
            <View>
                <Text> AppCenterIndex </Text>
                <Modal
                    isVisible={this.state.modalVisible}
                    backdropOpacity={0.2}
                    style={styles.modalStyle}
                    onBackdropPress={this.close} // 按下Android后退按钮时调用
                    onBackdropPress={this.close} // 按下背景时调用
                    // slidInUp 下方进入  fadeInRight右侧进入 fadeInLeft左侧进入
                    animationIn='fadeInLeft' // 模态框进入的方向

                    // slideOutDown 下方离开 fadeOutRight右侧离开 fadeOutLeft左侧离开
                    animationOut='fadeOutLeft' // 模态框离开的方向
                >
                    <View style={styles.minBox}>
                        <Text>modale</Text>
                        <TouchableOpacity
                            onPress={this.close}
                        >
                            <Text>关闭</Text>
                        </TouchableOpacity>
                    </View>
                    
                </Modal>

                <TouchableOpacity
                    onPress={this.openModal}
                >
                    <Text>openModal</Text>
                </TouchableOpacity>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    modalStyle: {
        margin: 0,
        alignItems:'flex-start',
        justifyContent: 'flex-start',
    },
    minBox: {
        paddingTop: 50,
        marginLeft: 20,
        flex: 1,
        backgroundColor: '#fff',
        width: 300,
        position: 'absolute',
        right: 0,
        top: 0,
        bottom: 0,
    },
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值