A lot of white space between items of a flat list in react native - react-native

I am trying to create a flat list in react native that displays an image as many times as the length of an array called "bools" (later, I will also display contents of bools in the flatlist). While I am able to create and display the flatlist, there is a lot of space between each item and above & below the first and last items, respectively. I can’t remove top and bottom margins completely because I want to add other elements above and below the flatlist. Other than this, I have tried every possible thing I could find online, but nothing is working.
Flat List:
<View style={{ marginTop: "10%", marginBottom: "20%" }}>
<FlatList
// contentContainerStyle={{ paddingBottom: "40%", paddingTop: "5%" }}
style={{ paddingBottom: "3%" }}
data={bools}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item, index }) => {
return (
<SafeAreaView style={styles.itemContainer}>
<View style={styles.iconStyle}>
<Image
source={someImage}
style={{ aspectRatio: 1, height: "8%" }}
/>
</View>
</SafeAreaView>
);
}}
/>
</View>
Styles:
styles = StyleSheet.create({
itemContainer: {
flex: 1,
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
height: "30%",
marginVertical: "3%",
//paddingTop: "1%",
//paddingBottom: "1%"
},
iconStyle: {
borderWidth: 1,
padding: 10,
borderRadius: 50,
borderColor: "#555",
marginRight: "5%",
resizeMode: "contain",
},
});
})
Thank you!

You can use SafeAreaView out rather then wrapping with all items And you can give style safearea as per your requirement.
<SafeAreaView>
<View style={{ marginTop: "10%", marginBottom: "20%" }}>
<FlatList
// contentContainerStyle={{ paddingBottom: "40%", paddingTop: "5%" }}
style={{ paddingBottom: "3%" }}
data={bools}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item, index }) => {
return (
<View style={styles.itemContainer}>
<View style={styles.iconStyle}>
<Image
source={someImage}
style={{ aspectRatio: 1, height: "8%" }}
/>
</View>
</View>
);
}}
/>
</View>
</SafeAreaView>

You have the spacing between your elements because of your styling properties.
In your FlatList just remove these properties and the space of above & below the first and last items will be gone. You have to remove these lines.
contentContainerStyle={{
paddingBottom: "40%",
paddingTop: "5%"
}}
style={{paddingBottom: '3%'}}
Similary the space between the items are due to the margin in the itemContainer of Styles Object. just remove this line and then spacing between the items will be gone as well
marginVertical: "3%"
Your Final Code should be something like this
<View>
<FlatList
data={bools}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => {
return (
<SafeAreaView style={styles.itemContainer}>
<View style={styles.iconStyle}>
<Image style={{aspectRatio: 1, height: '8%'}} />
</View>
</SafeAreaView>
);
}}
/>
</View>
const styles = StyleSheet.create({
itemContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
height: '30%',
backgroundColor: 'red',
//paddingTop: "1%",
//paddingBottom: "1%"
},
iconStyle: {
borderWidth: 1,
padding: 10,
borderRadius: 50,
borderColor: '#555',
marginRight: '5%',
resizeMode: 'contain',
backgroundColor: 'blue',
},
});

Related

Change the numColumns of the FlatList turns the direction horizontal

Enabling numColumns = {2}, turns the FlatList on horizontal direction, and with the Scroll blocked.
I want that stays in vertical direction with 2 items per row.
My FlatList
<FlatList
data={shipments}
keyExtractor={(item, index) => index.toString()}
showsVerticalScrollIndicator={false}
numColumns={2}
renderItem={({ item: shipmentsArray }) => {
return shipmentsArray.map((shipment) => {
return (
<View
key={shipment["delivery no."]}
style={{ alignItems: "center" }}
>
<TouchableOpacity
style={style.shipment}
>
<Text>Nº Pedido:</Text>
</TouchableOpacity>
</View>
);
});
}}
/>
Shipment style
shipment: {
width: 160,
height: 130,
borderColor: "gray",
borderWidth: 1,
borderRadius: 10,
justifyContent: "space-evenly",
paddingHorizontal: 20,
marginBottom: 20,
marginRight: 20,
backgroundColor: "#ebebeb",
flexDirection: "column",
}
Image of the app:

How to make full height my Flatlist Item in react native?

I have 5 items in FlatList.
I want my items render evenly in fullscreen(Top image to Bottom image).
If I change height or flex, It will shows 5 items in current backgroundImage space.
what should I change..?
<View
style={{
flex: 1,
flexDirection: 'column',
backgroundColor: 'black',
}}
>
<FlatList
style={{ flex: 1 }}
data={emotions}
renderItem={({ item }) => (
<ImageBackground
source={item.img}
style={{
width: '100%',
height: '100%',
justifyContent: 'center',
margin: 2,
alignContent: 'stretch',
}}
>
<Text
style={{
fontSize: 20,
textAlign: 'center',
color: 'white',
}}
onPress={() => this.handlePress(`${item.title}`)}
>
{item.title}
</Text>
</ImageBackground>
)}
keyExtractor={item => item.title}
numColumns={1}
></FlatList>
</View>
In render item can you try this
Please replace this
height: '100%'
with
flex:1

React Native; How to place spaces in FlatList

I am trying to build a flat list in react native but am having trouble keeping the specific list items from not touching. I tried to use the itemSeparatorComponent but the problem still persisted.
This is my FlatList component
<View style={{ position: "absolute", bottom: -35, marginLeft: 40 }}>
<FlatList
horizontal
data={this.state.hitRecords}
renderItem={({ item }) => (
<ArticleBox
title={item.title}
year={item.year}
time={item.time}
author={item.author}
/>
)}
itemSeparatorComponent = {()=> </View>}
/>
</View>
This is my ArticleBox component
const styles = StyleSheet.create({
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2
},
layout: {
textAlign: "center",
color: "#FFF"
},
input: {
color: "#ff8c00"
}
});
class ArticleBox extends Component<Props> {
render() {
return (
<View style={styles.box}>
<Text style={styles.layout}>
Time: <Text styles={styles.input}>{this.props.time}</Text>
</Text>
<Text />
<Text style={styles.layout}>Publication Year: {this.props.year}</Text>
<Text />
<Text style={styles.layout} numberOfLines = {3} >Publication Title: {this.props.title}</Text>
<Text />
<Text style={styles.layout}>Author: {this.props.author}</Text>
</View>
);
}
}
I am trying to get the list to behave like this
item item item item
but the list behaves like this
itemitemitemitem
Add marginLeft: 12 in your 'ArticleBox' Component. like this.
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2,
marginLeft: 12
}

Change the style of row in FlatList using SetNativeProps

<FlatList
data={this.state.bankDetail}
renderItem={({ item, index }) => this.bankImgView(item, index)}
keyExtractor={(item, index) => index}
horizontal={false}
numColumns={4}
/>
bankImgView(item, index) {
return (
<TouchableOpacity onPress={() => this.selectBank(item.image, item.text, index)}>
<View ref={"img"+index} style={{ backgroundColor: "white", marginTop: 2.5, justifyContent: "center", marginLeft: 6, alignItems: "center", flex: 1, marginBottom: 2.5 }}>
<View style={{ width: width / 4 - 8, height: 90, justifyContent: "center", alignItems: "center" }}>
<Image source={{ uri:item.image }} style={{ height: 60, width: 60 }} />
</View>
</View>
</TouchableOpacity>
)
}
selectBank(bankImage,bankName,index) {
this.refs["img"+index].setNativeProps({ backgroundColor: "red" });
}
I wanted to change the background color of a view using setNativeProps which is in flatList but it not changed , the error encounters "setNativeProps of undefined".So Please provide me the correct way for doing this.
Change selectBank function to
selectBank = (bankImage,bankName,index) => {
this.refs["img"+index].setNativeProps({ backgroundColor: "red" });
}
You are accessing references in a function where this is changed and this.refs["img"+index] is undefined.
So change it accordingly to es6 so that this is bind automatically.

ListEmptyComponent not taking full screen with flex 1 in React Native Section List

So I am using React Native Section List and following is my Code of ListEmptyContent
// define your styles
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
marginLeft: 10,
marginRight: 10,
},
imageStyle: {
width: 140,
height: 120,
},
titleStyle: {
fontSize: 14,
color: '#363a45',
},
subTitleStyle: {
fontSize: 12,
color: '#898d97',
},
});
// create a component
const GPEmtptyTransaction = ({ firstLine, secondLine }) => {
return (
<View style={styles.container}>
<Image source={images.emptyTransactionIcon} style={styles.imageStyle} />
<Text style={styles.titleStyle}>{firstLine}</Text>
<Text style={styles.subTitleStyle}>{secondLine}</Text>
</View>
);
};
But when EmptyTemplate is rendered it is rendered on Top and not stretching to full screen.
This works for me, apply flexGrow: 1 to contentContainerStyle
<FlatList
data={this.props.operations}
contentContainerStyle={{ flexGrow: 1 }}
ListEmptyComponent={<EmptyPlaceHolder />}
renderItem={this.renderOperationItem} />
For me what worked was adding some styles to the contentContainerStyle as well:
contentContainerStyle={{ flex: 1, justifyContent: 'center' }}
The SectionList complete setup on my end was:
<SectionList
showsVerticalScrollIndicator={false}
sections={filteredData}
keyExtractor={(item) => item.id.toString()}
renderItem={renderItem}
initialNumToRender={15}
contentContainerStyle={{ flex: 1, justifyContent: 'center' }}
ListEmptyComponent={() => (
<EmptyListComponent
icon={<Document />}
message={'Your roster is empty'}
/>
)}
/>
You can add contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }} prop to FlatList
I got success with the simple trick as below
import { Dimensions } from "react-native";
const SCREEN_HEIGHT = Dimensions.get("window").height;
than I declare the empty component
_listEmptyComponent = () => {
return (
<View
style={{
justifyContent: "center",
alignItems: "center",
height: SCREEN_HEIGHT , //responsible for 100% height
backgroundColor: "#ddd"
}}
>
<Text
style={{
justifyContent: "center",
alignItems: "center",
fontSize: 20
}}
>
No Contracts Found
</Text>
</View>
);
And at last Flatlist look like :
<FlatList
extraData={this.props.contracts}
data={this.props.contracts}
ListEmptyComponent={this._listEmptyComponent.bind(this)}
renderItem={({ item }) => (
<Text>{item.contractName}>
<Text/>
)}
keyExtractor={(item, index) => item.id}
/>