【问题标题】:I can't wrap a FlatList, it makes the FlatList height is over the wrapper我无法包装 FlatList,它使 FlatList 的高度超过了包装器
【发布时间】:2020-11-17 01:03:19
【问题描述】:

大家好!

我想在Modal 中显示我的通知数据并使用FlatList,我将react-native-modal 用于Modal 组件,在Modal 中我有3 个主要组件,例如header、@987654327 @ 和 footer

header 只是一个标题和它的底线

bodyFlatList

footer 是一个用于清除通知的按钮

我用View 组件再次包装这三个主要组件,并为此(View)组件设置maxHeight

body 是这里的主要问题,因为FlatList 的高度超过了我设置的maxHeightView 高度,很难解释和抱歉,但你可以看到底部的图片

重要的是,我不能在 Modal 中使用 flex:1,因为如果我使用 flex: 1,我使用的模块 (react-native-modal) 会使 FlatList 消失

图片

enter image description here

ModalNotificationShowcase.jsx 的代码

import React, {
  forwardRef,
  memo,
  useContext,
  useEffect,
  useImperativeHandle,
  useState,
} from 'react';
import {View, Dimensions, Pressable, FlatList} from 'react-native';
import {Divider, Button, Icon, List} from '@ui-kitten/components';
import {IconHelper, Text, Modal} from '../Helper';
import {useRealmInstance, useRealmObjects} from '../../hooks';
import moment from 'moment';
import util from '../../utils';
import ThemeContext from '../../themes/ThemeContext';

const NotificationItem = memo(props => {
  const {currentTheme} = props;
  const [bgColor, setBgColor] = useState(
    currentTheme === 'light' ? '#EDF1F7' : '#1A2138',
  );
  const notif = props.data;
  const realm = props.realm;

  return (
    <Pressable
      onPressIn={() =>
        setBgColor(currentTheme === 'light' ? '#E4E9F2' : '#151A30')
      }
      style={{
        padding: 12,
        marginBottom: 12,
        backgroundColor: bgColor,
        borderRadius: 4,
      }}
      onPress={props.onPress}
      onPressOut={() =>
        setBgColor(currentTheme === 'light' ? '#EDF1F7' : '#1A2138')
      }
    >
      <View style={{flexDirection: 'row'}}>
        <IconHelper
          color={notif.name === 'reminder' ? '#FF3D71' : '#0095FF'}
          name={
            notif.name === 'reminder'
              ? 'alert-triangle-outline'
              : 'info-outline'
          }
          style={{marginRight: 8}}
          onPress={() => console.log('Pressed')}
        />
        <Text size={14} style={{flex: 1, marginBottom: 4}}>
          {notif.message}
        </Text>
      </View>

      <View style={{flexDirection: 'row', alignItems: 'center'}}>
        <Text size={12} hint>
          {moment(notif.createdAt).fromNow()}
        </Text>
        <View style={{flex: 1, alignItems: 'flex-end'}}>
          <IconHelper
            size={20}
            color='#FF3D71'
            name='trash-2-outline'
            underlayColor='#E4E9F2'
            onPress={() => {
              realm.write(() => {
                realm.delete(notif);
              });
            }}
          />
        </View>
      </View>
    </Pressable>
  );
});

const NotificationShowcase = props => {
  const realm = useRealmInstance();
  const notifications = useRealmObjects('notifications');
  const {theme} = useContext(ThemeContext);

  return (
    <View
      style={{
        width: Dimensions.get('window').width - 48,
        maxHeight: Dimensions.get('window').height - 48,
        borderRadius: 4,
      }}
    >
      // =====
      // Header
      // =====
      <Text size={14} bold align='center' style={{padding: 12}}>
        Notifikasi
      </Text>
      <Divider />

      // =====
      // Body
      // =====
      <View style={{padding: 12, flexGrow: 1, overflow: 'hidden'}}>
        {notifications.isEmpty() ? (
          <Text
            size={14}
            align='center'
            hint
            style={{
              padding: 8,
              marginBottom: 12,
              backgroundColor: theme === 'light' ? '#EDF1F7' : '#1A2138',
              borderRadius: 4,
            }}
          >
            Belum ada notifikasi untuk saat ini
          </Text>
        ) : (

          // ======================
          // The main issue is here
          // ======================
          <FlatList
            data={notifications}
            initialNumToRender={0}
            maxToRenderPerBatch={1}
            updateCellsBatchingPeriod={120}
            numColumns={1}
            keyExtractor={item => item._id.toString()}
            renderItem={items => {
              const notif = items.item;
              return (
                <NotificationItem
                  data={notif}
                  realm={realm}
                  currentTheme={theme}
                />
              );
            }}
          />
        )}

        // =====
        // Footer
        // =====
        {!notifications.isEmpty() && (
          <View
            style={{
              justifyContent: 'center',
              alignItems: 'center',
            }}
          >
            <Button
              size='small'
              appearance='outline'
              accessoryLeft={props => (
                <Icon {...props} name='trash-2-outline' />
              )}
              onPress={() => {
                realm.write(() => {
                  realm.delete(notifications);
                });
              }}
            >
              Hapus semua
            </Button>
          </View>
        )}
      </View>
    </View>
  );
};

const ModalNotificationShowcase = forwardRef((props, ref) => {
  let Modal_ref;

  useImperativeHandle(ref, () => ({
    show: () => Modal_ref.show(),
    hide: () => Modal_ref.hide(),
  }));

  return (
    <Modal
      backdropStyle={{backgroundColor: 'rgba(9, 44, 76, .32)'}}
      onBackdropPress={() => Modal_ref.hide()}
      ref={refs => (Modal_ref = refs)}
    >
      <NotificationShowcase
        navigate={props.navigate}
        onPressNotifItem={() => Modal_ref.hide()}
      />
    </Modal>
  );
});

export default ModalNotificationShowcase;

【问题讨论】:

    标签: javascript react-native react-native-flatlist


    【解决方案1】:

    更新

    const NotificationShowcase = props => {
      return (
        <View
          style={{
            flex: 1,
            margin: 24,
            borderRadius: 4,
          }}
        >
          ...
          // =====
          // Body
          // =====
          <View style={{flex: 1}}>
            // ...
          </View>
        </View>
      );
    };
    

    【讨论】:

    • 感谢您的回答,但还是一样,最糟糕的是我无法滚动 FlatList
    • NotificationShowcase的正文&lt;View style={{padding: 12, flexGrow: 1, overflow: 'hidden'}}&gt;中删除溢出属性
    • 我愿意,它仍然是
    • 我不知道你为什么不能使用flex。根据我过去的经验,我在 Modal 内部使用了 flex
    • 我认为这是react-native-modal的问题
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2019-02-16
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多