简体   繁体   English

选择列表项时如何添加弹出模式反应本机并显示其详细信息

[英]How to add a popup modal when selecting a list item react native and show their details

As title says, I need to create a modal popup for every list item for the selected sizes to be displayed.正如标题所说,我需要为要显示的选定尺寸的每个列表项创建一个模式弹出窗口。 I'm using the react-native-popup-dialog but without results.我正在使用 react-native-popup-dialog 但没有结果。 Could you help me?你可以帮帮我吗? Here's my code:这是我的代码:

step1 = () => {
        return (
            <View style={{ flex: 8}}>
                <Text h3 style={{ ...styles.title, marginVertical: 10.0 }}>{this.state.user=="User"?"Size":"CargoSize"}</Text>
                <ScrollView>
                    <View style={{ marginHorizontal: 16.0 }}>{
                        this.state.size.map((l, i) => (
                            <ListItem key={i}  onPress={() => this.setState({sizeSelected: i, sizeName: l.title, sizeId: l.id})} underlayColor='transparent'
                            containerStyle={{backgroundColor: this.state.sizeSelected==i?'#F76858':'white', borderWidth: 1.0,
                            borderColor: '#707070', marginBottom: 10.0, paddingVertical: 5.0, paddingHorizontal: 40.0}}>
                                <ListItem.Content>
                                    <View style={{
                                        flexDirection: 'row', alignItems: 'center',
                                        justifyContent: 'center'
                                    }}>
                                        <Text style={styles.textSize}>{l.title}</Text>
                                        <Text style={{ fontSize: 16 }}>{l.example}</Text>
                                    </View>
                                </ListItem.Content>
                            </ListItem>
                        ))
                    }</View>
                </ScrollView>
            </View>
        );
    }

I believe you can declare a single modal in your list component and show or hide the modal when the user presses the listItem.我相信您可以在列表组件中声明一个模式,并在用户按下 listItem 时显示或隐藏模式。 Also, make the pressed item active and pass the item to your modal so that the modal can show the details of the active item.此外,使按下的项目处于活动状态并将项目传递给您的模式,以便模式可以显示活动项目的详细信息。

App.js应用程序.js

export default function App() {
  const [modalVisible, setModalVisible] = React.useState(false);
  const [activeItem, setActiveItem] = React.useState(null);

  const onPress = (item) => {
    setActiveItem(item)
    setModalVisible(true)
  }

  const renderItem = ({ item }) => (
    <TouchableOpacity onPress={()=>onPress(item)}>
      <Item title={item.title} />
    </TouchableOpacity>
  );

  return (
    <View style={styles.container}>
      <FlatList
        data={DATA}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
      <Popup modalVisible={modalVisible} setModalVisible={setModalVisible} activeItem={activeItem} />
    </View>
  );
}

Modal.js模态.js

export default function Popup({modalVisible, setModalVisible, activeItem}) {
  return (
    <View style={styles.centeredView}>
      <Modal
        animationType="slide"
        transparent={true}
        visible={modalVisible}
        onRequestClose={() => {
          Alert.alert('Modal has been closed.');
        }}>
        <View style={styles.centeredView}>
          <View style={styles.modalView}>
            <Text style={styles.modalText}>{activeItem?.title}</Text>

            <TouchableHighlight
              style={{ ...styles.openButton, backgroundColor: '#2196F3' }}
              onPress={() => {
                setModalVisible(!modalVisible);
              }}>
              <Text style={styles.textStyle}>Hide Modal</Text>
            </TouchableHighlight>
          </View>
        </View>
      </Modal>
    </View>
  );
}

Here is a working demo for you.是为您准备的工作演示。

I made something similar in my react-native project and do this:我在我的 react-native 项目中做了类似的事情并这样做:

  1. Made with {useState} the variables i need in my modal, example, the name of my component用 {useState} 制作我在模态中需要的变量,例如,我的组件名称
  2. Wrap the ListItem component in a Pressable component将 ListItem 组件包装在 Pressable 组件中
  3. Put the onPress={() => {//Put your code here}} function, inside you will put the SetVariable of the variable you want in the modalonPress={() => {//Put your code here}} function,里面你会把你想要的变量的 SetVariable 放在模态中
  4. The Pressable will update the variable with the wich you want in the modal Pressable 将在模态中用你想要的更新变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应本机中单击平面列表项时将值传递给模态并显示模态 - How to pass a value to modal and show modal when click on flatlist item in react native React Native / Redux-渲染项目列表,如何通过ID为每个项目请求数据以显示详细信息而不覆盖先前请求的数据? - React Native / Redux - rendering list of items, how to request data by id for each item to show details and not override previous requested data? 反应原生 Picker 项目列表绑定与获取请求并选择项目 - React native Picker item list binding with fetch request and selecting an item 如何传递数据项以反应本机模式 - How to pass data item to react native modal 当我按下导航器上的按钮图标时如何应用,模式将使用本机反应自动弹出 - How to apply when I press the button icon on the navigator the modal will automatically popup using react native 将数字添加到列表项 React-native - Add numbers to list item React-native 反应单击项目以显示详细信息 - React click on item to show details 当我在React Native中单击列表项时如何更改文本? - how to change text when i click list item in react native? 如何在 Semantic-react-ui 中的模态屏幕上显示弹出窗口? - How to show the Popup over the Modal screen in Semantic-react-ui? 在React Native中单击项目时如何关闭模态列表并从平面列表返回值? - How to close modal and return value from flatlist when click on item in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM