react-native-modal弹窗中FlatList高度溢出外层maxHeight容器
React Native Modal 内 FlatList 突破 maxHeight 边界溢出问题
场景说明
我基于react-native-modal库实现通知弹窗,需要在弹窗内展示通知列表,并用FlatList完成列表渲染。弹窗内部按功能拆分为三个独立模块:
header:带底部分割线的标题栏body:承载通知列表的FlatList渲染区域footer:放置“清空全部通知”操作按钮的操作区
我把三个模块全部包裹在一个外层View容器中,为容器设置了maxHeight的高度限制,但出现了核心布局异常:
FlatList的高度完全不受外层容器的maxHeight约束,撑破容器边界出现布局溢出,可参考对应的异常效果截图。
实际开发中我没法在弹窗内部给任何元素应用
flex:1样式:只要给弹窗内元素设置flex:1,当前版本的react-native-modal就会导致内部的FlatList直接消失,完全无法渲染。
现有实现代码
以下是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> ) : ( // ====================== // 问题核心区域 // ====================== <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;
内容的提问来源于Stack Exchange,提问作者Firmansyah
相关产品推荐
相关产品推荐

