How to remove space between flexes in react native?(flexWrap) - react-native

How to remove space between flex. (spaces is marked in green)
I am currently using flex-wrap: 'wrap', and flat list with 2 columns. I am trying to archive something like google keep
My code:-
<FlatList
data={[
{ storeName: 'The Look' },
{ storeName: 'STYLO' },
{ storeName: 'Bombshell studio' },
{ storeName: 'STYLO1' },
{ storeName: 'The Look2' },
{ storeName: 'STYLOe' },
{ storeName: 'STYdLO1' },
{ storeName: 'Thed Look2' },
{ storeName: 'STdYLOe' },
]}
numColumns={2}
columnWrapperStyle={{
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
alignItems: 'flex-start',
paddingLeft: 15,
}}
containerStyle={{
width: '100%',
}}
renderItem={({ item }) => (
<ARecentBooking key={item.storeName} storeName={item.storeName} />
)}>
</FlatList>

You can implement the UI as shown below
import * as React from 'react';
import { Text, View, StyleSheet, FlatList, Dimensions } from 'react-native';
import Constants from 'expo-constants';
const DeviceWidth = Dimensions.get('window').width;
export default function App() {
const renderItem = ({ item }) => (
<View style={styles.Card}>
{/* ALl Other COde here...Like the 15 AUG and everything */}
</View>
);
return (
<View style={styles.container}>
<FlatList
data={[
{ storeName: 'The Look' },
{ storeName: 'STYLO' },
{ storeName: 'Bombshell studio' },
{ storeName: 'STYLO1' },
{ storeName: 'The Look2' },
{ storeName: 'STYLOe' },
{ storeName: 'STYdLO1' },
{ storeName: 'Thed Look2' },
{ storeName: 'STdYLOe' },
]}
numColumns={2}
columnWrapperStyle={{
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
}}
containerStyle={{
width: '100%',
}}
renderItem={renderItem}></FlatList>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: 'white',
},
Card: {
flex: 1,
backgroundColor: '#FFEAEC',
minHeight: 150,
margin: 10,
borderRadius: 12,
maxWidth: DeviceWidth / 2 - 20,
},
});
Here is a Snack for the same.
Here's a Screenshot of the result.

method 1:- I manage to get the result with some workaround:-
<ScrollView style={{flexDirection:'row'}}>
//put two faltlist
<Flatlist scrollEnabled={false}/>
<Flatlist scrollEnabled={false}/>
<ScrollView/>
put items of the odd index in the first flatlist
and even items in the second flatList
function separateArrays(array) {
let arr1=[]
let arr2=[]
for(i=0; i<array.length; i++) {
if(i%2 === 0) {
arr1.push(array[i])
}
else{
arr2.push(array[i])
}
}
return [arr1, arr2]
}
method 2:-It also can be done using .map function
<ScrollView>
<View style={{ flexDirection: 'row', paddingLeft: 15 }}>
<View>
{arr1.map((item, index) => {
return <ARecentBooking key={item.storeName} item={item} />
})}
</View>
<View>
{arr2.map((item, index) => {
return <ARecentBooking key={item.storeName} item={item} />
})}
</View>
</View>
</ScrollView>

Related

React native draggle section list

I am want to make a draggable section list. Where I can drag one item from the section list and move it to another section.
I am try to do this using.
react-native-draggable-flatlist
Issue is that when I select an item and drage it. It takes the whole list.
import React, { FC, useState } from "react";
import { Text, View, StyleSheet, TouchableOpacity } from "react-native";
import DraggableFlatList, {
RenderItemParams,
ScaleDecorator,
} from "react-native-draggable-flatlist";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { Data } from "./data";
import Example from "./example";
interface Item {
header: string;
data: string[];
}
const DATA: Item[] = [
{
header: "A",
data: ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"],
},
{
header: "B",
data: ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10"],
},
];
interface IList {
item: string;
selected: (item: string) => void;
active: boolean;
}
const List: FC<IList> = ({ item, selected, active }) => {
const getSelected = () => {
console.log("item", item);
selected(item);
};
return (
<TouchableOpacity
style={[
styles.draggableContainer,
{
backgroundColor: active ? "blue" : "white",
},
]}
onLongPress={getSelected}
onPress={getSelected}
>
<Text style={styles.text}>{item}</Text>
</TouchableOpacity>
);
};
export default function App() {
const renderContent = (item: Item, drag: () => void, isActive: boolean) => {
return (
<View style={styles.item}>
{item?.data?.map((seat) => (
<List key={seat} item={seat} selected={drag} active={isActive} />
))}
</View>
);
};
const renderSectionHeader = (section: Item) => (
<View style={styles.header}>
<Text style={styles.headerText}>{section.header}</Text>
</View>
);
const renderItem = ({ item, drag, isActive }: RenderItemParams<Item>) => (
<View style={{ flex: 1 }}>
<View>{renderSectionHeader(item)}</View>
<View style={{ flex: 1 }}>{renderContent(item, drag, isActive)}</View>
</View>
);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<View style={styles.container}>
<DraggableFlatList
data={DATA}
renderItem={renderItem}
keyExtractor={(item, index) => `draggable-item-${item?.header}`}
/>
</View>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
item: {
borderRadius: 5,
marginVertical: 8,
marginHorizontal: 16,
},
header: {
backgroundColor: "#f9c2ff",
height: 30,
justifyContent: "center",
paddingLeft: 10,
},
headerText: {
fontSize: 16,
fontWeight: "bold",
},
text: {
fontSize: 18,
},
draggableContainer: {
flexDirection: "column",
flex: 1,
paddingVertical: 10,
},
draggable: {
flex: 1,
},
container: {
flex: 1,
},
});
I have tryed using draxList but I was getting peformace issues. Draggable flat list has the best performance.
I have so many times react-native-draggable-flatlist
and I have the code for this below.
it works for me fine you can update your code like this.
<DragableFlatlist
ItemSeparatorComponent={() => (
<View style={styles.itemSeprator} />
)}
data={drag}
keyExtractor={(item) => item.id}
nestedScrollEnabled
onDragEnd={({ data }) => setDrag(data)}
renderItem={renderItemDrag}
showsVerticalScrollIndicator={false}
/>

React Native Scrollview - scroll one by one (items with different widths)

I have a horizontal ScrollView, all items have different widths
Is there a way to scroll one by one, so it would stop in the center of each item?
If the first answer to question is yes, then is there a way to know the index of item (that is centered), so I can change the index of selected dot?
https://snack.expo.dev/#drujik/horizontal-scroll-diff-widths
Example for: https://i.stack.imgur.com/thKLv.gif
I'm using FlatList and its props.
More information: https://reactnative.dev/docs/flatlist
import * as React from "react";
import { StyleSheet, Dimensions, FlatList, Text, View } from "react-native";
const data = [
{
text: "Page1",
width: 300,
},
{
text: "Page2",
width: 250,
},
{
text: "Page3",
width: 200,
},
];
const DOT_SIZE = 8;
const { width } = Dimensions.get("window");
export default function App() {
const [indexDot, setIndexDot] = React.useState(0);
const onChangeDot = (event) => {
setIndexDot(Math.ceil(event.nativeEvent.contentOffset.x / width));
};
const renderPagination = React.useMemo(() => {
return (
<View style={styles.wrapPagination}>
{data.map((_, index) => {
return (
<View
key={index}
style={[
styles.dot,
{
backgroundColor:
indexDot === index ? "#E7537A" : "rgba(0, 0, 0, 0.3)",
},
]}
/>
);
})}
</View>
);
}, [data?.length, indexDot]);
const renderItem = ({ item }) => (
<View style={styles.wrapItem}>
<View
style={{
...styles.item,
width: item.width,
}}
>
<Text>{item.text}</Text>
</View>
</View>
);
return (
<>
<FlatList
horizontal
pagingEnabled
disableIntervalMomentum
showsHorizontalScrollIndicator={false}
data={data}
renderItem={renderItem}
scrollEventThrottle={200}
onMomentumScrollEnd={onChangeDot}
/>
{renderPagination}
</>
);
}
const styles = StyleSheet.create({
wrapPagination: {
flexDirection: "row",
flex: 1,
justifyContent: "center",
alignItems: "center",
},
dot: {
height: DOT_SIZE,
width: DOT_SIZE,
borderRadius: DOT_SIZE / 2,
marginHorizontal: 3,
},
wrapItem: {
width,
padding: 20,
alignItems: "center",
justifyContent: "center",
},
item: {
alignItems: "center",
justifyContent: "center",
borderWidth: 1,
},
});
You can achieve this by react-native-snap-carousel and For dot, You can use pagination.
import Carousel, { Pagination } from 'react-native-snap-carousel';
const [active, setActive] = useState(0)
const [data, setData] = useState([])
<Carousel
keyExtractor={(_, index) => `id-${index}`}
data={data}
renderItem={renderItem}
onSnapToItem={setActive} />
<Pagination
dotsLength=length}
activeDotIndex={active}
dotStyle={{ backgroundColor: colors.blue }}
inactiveDotStyle={{ backgroundColor: colors.gray }}
inactiveDotScale={1}
inactiveDotOpacity={1} />

How to create a drag and drop nested list in React Native

I am fairly new to React native, and I am looking for a way to have a Drag and Drop Nested List. Basically, I need to create a ToDo list divided in groups, in which the ToDos' order can be changed not only within groups but also among them. I managed to separatly create both a drag & drop list (using the "draggable Flatlist" components) and a nested list, but I am struggling in combining them.
Does anyone solved the issue or knows some kind of reusable component? Thank you.
Try the following:
import React, { useState, useCallback, Component } from 'react';
import { View, TouchableOpacity, Text, SafeAreaView, ScrollView } from 'react-native';
import DraggableFlatList, { RenderItemParams, } from 'react-native-draggable-flatlist';
const Goal_data = [
{
key: "0",
label: "Group",
backgroundColor: "#ababab",
},
{
key: "1",
label: "Group",
backgroundColor: "#ababab",
}
]
const Goal_data1 = [
{
key: "0",
label: "Task",
},
{
key: "1",
label: "Task",
}
]
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
data: Goal_data,
data1: Goal_data1,
scrollEnabled: true
}
}
onEnableScroll = (value) => {
this.setState({
enableScrollViewScroll: value,
});
}
renderItem1 = ({ item, index, drag, isActive }) => {
console.log('index', item)
return (
<TouchableOpacity
style={{
height: 70,
backgroundColor: isActive ? "blue" : item.backgroundColor,
alignItems: "center",
justifyContent: "center"
}}
onLongPress={drag}
>
<Text
style={{
fontWeight: "bold",
color: "white",
fontSize: 20
}}
>
{item.label}
</Text>
</TouchableOpacity>
);
};
plusdata = (data) => {
let d = this.state.data1;
const newRecord = {
key: "2",
label: "Task",
};
this.setState({
data1: [...d, newRecord]
})
}
render() {
return (
<SafeAreaView style={{ flex: 1, }}>
<ScrollView>
<View style={{ backgroundColor: 'aeaeae', flex: 1, paddingHorizontal: 30 }}>
<Text>Hello</Text>
<DraggableFlatList
data={Goal_data}
debug={true}
extraData={Goal_data}
keyExtractor={(item, index) => `draggable-item-${item.key}`}
//onMoveBegin={() => this.setState({ scrollEnabled: false })}
onDragEnd={({ data }) => this.setState({ data: data })}
renderItem={({ item, index, drag, isActive }) => {
console.log('index', item)
return (
<TouchableOpacity
style={{
backgroundColor: isActive ? "blue" : item.backgroundColor,
//alignItems: "center",
justifyContent: "center",
marginVertical: 20
}}
onLongPress={drag}
>
<View style={{ backgroundColor: 'aeaeae', borderColor: '#000', borderWidth: 1, paddingHorizontal: 30 }}>
<Text>{item.label}{index}</Text>
<DraggableFlatList
data={this.state.data1}
extraData={this.state.data1}
debug={true}
keyExtractor={(item, index) => `draggable-item-${index}`}
//onDragEnd={({ data }) => this.setState({ data: data })}
renderItem={({ item, index, drag, isActive }) => {
console.log('index', item)
return (
<TouchableOpacity
style={{
height: 30,
borderBottomWidth: 1,
backgroundColor: isActive ? "blue" : item.backgroundColor,
alignItems: "center",
justifyContent: "center"
}}
onLongPress={drag}
>
<Text
style={{
fontWeight: "bold",
color: "white",
fontSize: 20
}}
>
{item.label}{index}
</Text>
</TouchableOpacity>
);
}}
/>
<TouchableOpacity style={{ marginTop: 50, alignSelf: 'center' }} onPress={() => this.plusdata(Goal_data1)}>
<Text>Add</Text>
</TouchableOpacity>
</View>
</TouchableOpacity>
);
}}
/>
</View>
</ScrollView>
</SafeAreaView>
)
}
}

FlatList with ScrollTo in React Native

I'm new to React Native, I'm trying to make a FlatList in an application with Expo that will pull some categories of products from a Json, I managed to make the FlatList and also a Json simulation for testing, however I wish that by clicking on any item in the FlatList it directs the Scroll to the respective section, the same when using anchor with id in HTML.
For example: FlatList will display the categories: Combo, Side Dishes, Hot Dog, etc. For each category that is displayed by FlatList I have already created a View that will display products in this category.
What I want to do:
When you click on a category displayed by FlatList the Scroll scrolls to the View that displays the products of this category, that is, if you click on Combo the page scrolls to the View that displays the products of the Combo category, if you click on Side Dishes the page scrolls until the View that displays the products in the Side Dishes category, follows my code:
Follow my code: (it can be simulated here: https://snack.expo.io/#wendell_chrys/06944b)
import React, { useEffect, useState } from 'react';
import { View, Image, ImageBackground, Text, StyleSheet, ScrollView, Dimensions, FlatList, SafeAreaView, TouchableOpacity } from 'react-native';
import { AppLoading } from 'expo';
import Constants from 'expo-constants';
const DATA = [
{
id: "1",
title: "Combos",
categorie: "section1",
},
{
id: "2",
title: "Side Dishes",
categorie: "section2",
},
{
id: "3",
title: "Hot Dog",
categorie: "section3",
},
{
id: "4",
title: "Desserts",
categorie: "section4",
},
{
id: "5",
title: "Drinks",
categorie: "section5",
},
];
const renderItem = ({ item }) => {
return (
<Item
item={item}
/>
);
};
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} >
<Text style={styles.itenscategoria}>{item.title}</Text>
</TouchableOpacity>
);
export default function App() {
return (
<View style={styles.container}>
<View style={styles.categories}>
<FlatList
data={DATA}
horizontal
showsHorizontalScrollIndicator={false}
renderItem={renderItem}
keyExtractor={(item) => item.id}
/>
</View>
<ScrollView>
<View style={styles.section1}>
<Text>Combos</Text>
</View>
<View style={styles.section2}>
<Text>Side Dishes</Text>
</View>
<View style={styles.section3}>
<Text>Hot Dog</Text>
</View>
<View style={styles.section4}>
<Text>Desserts</Text>
</View>
<View style={styles.section5}>
<Text>Drinks</Text>
</View>
</ ScrollView>
</View >
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#000',
padding: 8,
},
categories: {
backgroundColor: '#FFFFFF',
width: '100%',
height: 50,
top: 10,
marginBottom:20,
},
itenscategoria: {
padding:15,
border: 1,
borderRadius:25,
marginRight:10,
},
section1: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
section2: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
section3: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
section4: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
section5: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
});
You can make use of scrollToIndex of FlatList
See complete code below, or the snack here.
import React, { useEffect, useState, useRef } from 'react';
import { View, Image, ImageBackground, Text, StyleSheet, ScrollView, Dimensions, FlatList, SafeAreaView, TouchableOpacity } from 'react-native';
import { AppLoading } from 'expo';
import Constants from 'expo-constants';
const DATA = [
{
id: "1",
title: "Combos",
categorie: "section1",
},
{
id: "2",
title: "Side Dishes",
categorie: "section2",
},
{
id: "3",
title: "Hot Dog",
categorie: "section3",
},
{
id: "4",
title: "Desserts",
categorie: "section4",
},
{
id: "5",
title: "Drinks",
categorie: "section5",
},
];
export default function App() {
const flatListRef = useRef();
const handleItemPress = (index) => {
flatListRef.current.scrollToIndex({ animated: true, index });
};
const renderItem = ({ item, index }) => {
return (
<Item
item={item}
onPress={() => handleItemPress(index)}
/>
);
};
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} >
<Text style={styles.itenscategoria}>{item.title}</Text>
</TouchableOpacity>
);
const renderDetails = ({ item, index }) => (
<View style={styles.section}>
<Text>{item.title}</Text>
</View>
);
return (
<View style={styles.container}>
<View style={styles.categories}>
<FlatList
data={DATA}
horizontal
showsHorizontalScrollIndicator={false}
renderItem={renderItem}
keyExtractor={(item) => item.id}
/>
</View>
<FlatList
ref={flatListRef}
data={DATA}
renderItem={renderDetails}
keyExtractor={(item) => item.id}
/>
</View >
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#000',
padding: 8,
},
categories: {
backgroundColor: '#FFFFFF',
width: '100%',
height: 50,
top: 10,
marginBottom:20,
},
itenscategoria: {
padding:15,
border: 1,
borderRadius:25,
marginRight:10,
},
section: {
marginTop: 10,
backgroundColor: '#ccc',
width: '100%',
height: 200,
borderRadius: 10,
},
});

Change color after click on TouchableHighlight in react native

I have done a many research on the color change after press/click. Finally
I got this script for change the state and put it in the TouchableHighlight. But When i clicked on that, only the "underlayColor={'gray'}" is working. Can i get some idea ?
here is my code
import React, { Component } from 'react';
import {
StyleSheet,
Text,
StatusBar ,
TouchableOpacity,
View,
FlatList,
ActivityIndicator,
TouchableHighlight,
PropTypes,
Image,
Alert
} from 'react-native';
import Logo from '../components/Logo';
import Form from '../components/Front';
import {Actions} from 'react-native-router-flux';
export default class Login extends Component<{}> {
constructor() {
super();
this.state = {
dataSource: {},
pressed: false
};
}
izijackpotconfirm() {
Actions.izijackpotconfirm()
}
componentDidMount() {
var that = this;
let items = Array.apply(null, Array(25)).map((v, i) => {
return { id: i+1 };
});
that.setState({
dataSource: items,
});
}
static navigationOptions = {
title: "Izi Jackpot",
headerStyle: {
backgroundColor: "#354247"
},
headerTintColor: "#fff",
headerTitleStyle: {
fontWeight: "bold"
}
};
render() {
var jackpotNumbers = [];
let btn_class = this.state.black ? "NormalSet" : "SelectedSet";
return(
<View style={styles.container}>
<View style={styles.middlecontainer}>
<Text style={styles.logoText}>Please Select 5 Numbers and Submit</Text>
</View>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => (
<View style={{ flex: 1, flexDirection: 'column', margin: 1 }}>
<TouchableHighlight
onPress={() => { Alert.alert(this.state.pressed) }}
style={[
styles.iziPizi,
this.state.pressed ? { backgroundColor: "blue" } : {}
]}
onHideUnderlay={() => {
this.setState({ pressed: false });
}}
onShowUnderlay={() => {
this.setState({ pressed: true });
}}
underlayColor={'gray'}
>
<Text style={styles.buttonText}>{ item.id}</Text></TouchableHighlight>
</View>
)}
//Setting the number of column
numColumns={5}
keyExtractor={(item, index) => index}
/>
<View style={styles.middlecontainer}>
<TouchableOpacity style={styles.button} onPress={this.izijackpotconfirm} >
<Text style={styles.buttonText}>Submit</Text>
</TouchableOpacity>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container : {
backgroundColor:'#6F9000',
justifyContent: 'center',
flex: 1,
paddingTop: 30,
},
middlecontainer: {
textAlign: 'center',
alignItems: 'center',
justifyContent :'center',
flex:1
},
signupTextCont : {
flexGrow: 1,
alignItems:'flex-end',
justifyContent :'center',
paddingVertical:16,
flexDirection:'row'
},
signupText: {
color:'rgba(255,255,255,0.6)',
fontSize:16
},
signupButton: {
color:'#ffffff',
fontSize:16,
fontWeight:'500'
},
iziPizi: {
width: 55,
padding: 15,
margin: 5,
borderRadius: 80,
borderWidth: 2,
borderColor: '#FFFFFF',
flex:1
},
iziPiziPress: {
width: 55,
padding: 15,
margin: 5,
backgroundColor:'#1c313a',
borderRadius: 80,
borderWidth: 2,
borderColor: '#FFFFFF',
flex:1
},
button: {
width:300,
backgroundColor:'#1c313a',
borderRadius: 25,
marginVertical: 10,
paddingVertical: 13
},
buttonText: {
fontSize:16,
fontWeight:'500',
color:'#ffffff',
textAlign:'center'
},
logoText : {
color:'#FFFFFF',
fontSize: 16,
fontWeight: '500',
alignItems: 'center',
justifyContent:'center',
},
imageThumbnail: {
justifyContent: 'center',
alignItems: 'center',
height: 100,
},
});
One more thing to say that, i have used FlatList here. Please help on this. Thanks in advance.
The problem is in how you work with the styles inside the Touchable. My advice is to create 2 styles that contain the changes:
style={
this.state.pressed ? styles.pressed : styles.iziPizi
}
Inside the touchable of course:
<TouchableHighlight
onPress={() => { Alert.alert(this.state.pressed) }}
style={
this.state.pressed ? styles.pressed : styles.iziPizi
}
onHideUnderlay={() => {
this.setState({ pressed: false });
}}
onShowUnderlay={() => {
this.setState({ pressed: true });
}}
underlayColor={'gray'}
>
yes. there was a problem in FlatList. but i dont know what is its problem.
use ListItem of native-base instead.
remember that if you want to use ListItem, in constructor change
this.state = {
dataSource: {},
...
}
to
this.state = {
dataSource: [],
}
use array insead. here is a sample code for you.
<View style={{ flex: 1, flexDirection: 'column', margin: 1 }}>
<List>
{this.state.dataSource.map( item =>
<ListItem>
<TouchableHighlight
onPress={() => {}}
onShowUnderlay={this.onPress.bind(this)}
>
<Text style={styles.buttonText}>{ item.id}</Text>
</TouchableHighlight>
</ListItem> )
}
</List>
</View>
Also define onPress method.
another problem in your code is this that you setState of pressed.
it results that all of your element style will be changed. so all of your button backgroundColor will be changed.
so you must define pressed flag for every element in your array and change this flag