React Native 尝试设置模态、暗背景的不透明度

6pp0gazn  于 2022-11-17  发布在  React
关注(0)|答案(1)|浏览(173)

我有问题的不透明度。试图有一点暗淡时,模态被触发,它暗淡的背景与某种形式的不透明度和所有。我怎么能暗淡的背景时,模态被触发?
图像看起来是这样的:

这是我的源代码

import {
  ImageBackground,
  Modal,
  ScrollView,
  StyleSheet,
  Text,
  TextInput,
  TouchableOpacity,
  View,
} from 'react-native';
import {Picker} from '@react-native-picker/picker';
import React, {useState} from 'react';
import {useNavigation} from '@react-navigation/native';

const LocalPayments = () => {
  const navigation = useNavigation();
  const [modalVisible, setModalVisible] = useState(false);

  return (
    <View style={styles.container}>
      <View style={styles.space} />
      <ScrollView
        contentContainerStyle={{
          justifyContent: 'space-between',
          alignItems: 'center',
        }}>
        <ImageBackground
          source={{
            uri: 'asset:/logo/bg.JPG',
          }}
          imageStyle={{borderRadius: 6}}
          style={{
            top: -30,
            paddingTop: 95,
            alignSelf: 'center',
            width: 328,
            height: 115,
            borderadius: 9,
            justifyContent: 'center',
            alignSelf: 'center',
            alignItems: 'center',
          }}>
          <View>
            <Text style={styles.accText}>Wallet Balance</Text>
            <Text style={styles.text}> 250,000 </Text>
          </View>
        </ImageBackground>

        <View
          style={{
            borderRadius: 5,
            borderWidth: 1,
            overflow: 'hidden',
            height: 35,
            padding: 0,
            borderColor: '#00BB23',
          }}>
          {
            <Picker
              style={{
                width: 300,
                height: 55,
                borderBottomWidth: 1,
              }}
              itemStyle={{
                fontSize: 25,
                fontFamily: 'Poppins-Medium',
              }}>
              <Picker.Item label="Select Bank" value="accNum" />
            </Picker>
          }
        </View>

        <TextInput
          placeholder="Destination Account"
          onChangeText={creditAccount => this.setState({creditAccount})}
          style={styles.input}
        />

        <TextInput
          placeholder=" Amount"
          onChangeText={amount => this.setState({amount})}
          style={styles.input}
        />

        <TextInput
          placeholder=" Narration"
          onChangeText={description => this.setState({description})}
          style={styles.input}
        />

        <View
          style={{
            borderRadius: 5,
            borderWidth: 1,
            overflow: 'hidden',
            height: 35,
            padding: 0,
            top: 10,
            borderColor: '#00BB23',
          }}>
          {
            <Picker
              style={{
                width: 300,
                height: 55,
                borderBottomWidth: 1,
              }}
              itemStyle={{
                fontSize: 25,
                fontFamily: 'Poppins-Medium',
              }}>
              <Picker.Item label="Currency" value="accNum" />
              <Picker.Item label="NGN" value="NGN" />
            </Picker>
          }
        </View>

        <TouchableOpacity
          onPress={() => {
            setModalVisible(true);
          }}
          style={styles.button}>
          <Text style={styles.loginbtn}> Transfer </Text>
        </TouchableOpacity>
        <Modal
                hasBackdrop={true}
                backdropOpacity={0.2}
                backdropColor="black"
          transparent
          visible={modalVisible}
          onRequestClose={() => setModalVisible(false)}>
          <View style={styles.modal}>
            <Text>Hello From Modal</Text>
            <TouchableOpacity>
              <Text>Modal! Modal!</Text>
            </TouchableOpacity>
          </View>
        </Modal>
      </ScrollView>
    </View>
  );
};

export default LocalPayments;

const styles = StyleSheet.create({
  container: {
    paddingTop: 40,
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
  },

  modal: {
    top: '50%',
    height: '50%',
    backgroundColor:'#fff'
  },
  accText: {
    top: -50,
    paddingTop: 10,
    justifyContent: 'center',
    alignItems: 'center',
    fontFamily: 'Poppins-Medium',
    fontSize: 12,
    color: 'white',
    textAlign: 'center',
  },
  text: {
    top: -50,
    fontSize: 20,
    color: 'white',
    textAlign: 'center',
    fontFamily: 'Poppins-Bold',
  },
  input: {
    top: 10,
    width: 300,
    height: 55,
    margin: 10,
    fontSize: 12,
    borderColor: '#00BB23',
    fontFamily: 'Poppins-Bold',
    borderRadius: 5,
    borderWidth: 1,
    marginBottom: 30,
  },

  button: {
    marginTop: 40,
    width: 150,
    height: 50,
    padding: 10,
    borderRadius: 10,
    backgroundColor: '#00BB23',
    alignItems: 'center',
  },

  Regbutton: {
    width: 150,
    height: 52,
    padding: 10,
    borderRadius: 10,
    backgroundColor: '#ffffff',
    alignItems: 'center',
    borderWidth: 2,
    borderColor: '#030303',
  },

  loginbtn: {
    color: '#ffff',
    fontSize: 15,
    fontFamily: 'Poppins-Medium',
  },

  AccountBalance: {
    fontSize: 13,
    fontWeight: 'bold',
    textAlign: 'left',
  },

  loginbtn2: {
    color: '#030303',
    fontSize: 20,
    fontWeight: 'bold',
  },

  logo: {
    width: 150,
    height: 150,
  },

  space: {
    top: 10,
    width: 10,
    height: 20,
  },

  space2: {
    width: 10,
    height: 10,
  },

  imageStyle: {
    flexDirection: 'row',
    justifyContent: 'center',
    padding: 5,
    margin: 2,
    height: 15,
    width: 15,
    resizeMode: 'stretch',
    marginBottom: 8,
    marginTop: 8,
    alignItems: 'center',
  },
});

一直试图在没有任何形式的帮助下解决这个问题。请帮助这里是需要的。

xqk2d5yq

xqk2d5yq1#

请尝试以下操作:
创建此单独的文件:BackgroundOpacity.js

import React, {memo} from 'react';
import {Platform, View} from 'react-native';

const BackgroundOpacity = ({display}) => {
  return (
    <View
      style={{
        display: Platform.OS === 'ios' ? 'none' : display ? 'flex' : 'none'
        flex: 1,
        position: 'absolute',
        top: 0,
        height: '200%'
        backgroundColor: 'black',
        opacity: 0.35,
        left: 0,
        right: 0,
        top: 0,
        marginTop: -200,
        zIndex: 3
      }}
    />
  );
};

export default memo(BackgroundOpacity);

然后像这样实现它:

.... some imports

 const LocalPayments = () => {
  const navigation = useNavigation();
  const [modalVisible, setModalVisible] = useState(false);

  return (
    <View style={styles.container}>
     <BackgroundOpacity
    display={
      Platform.OS === 'ios'
        ? false
        : modalVisible
    }
  />
      <View style={styles.space} />
      <ScrollView
        contentContainerStyle={{
          justifyContent: 'space-between',
          alignItems: 'center',
        }}>
    ... Rest of your code

所以我们创建了一个可重用的组件,当你有一个模态的时候,它可以被添加到任何地方。但是问题是,为什么我们在使用这个backgroundOpacity的时候,我们要检查它总是一个android设备呢?
由于某些原因,在iOS上,每当你有一个打开的模态,它会自动添加一个背景不透明度自己。

相关问题