How can I create a resizable layout in react native? Like this:
Here is a demo but for ReactJS: https://leefsmp.github.io/Re-Flex/index.html
flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.
The styling code of the flexbox layout in React Native is written in JavaScript. The one designed for a web browser is developed via CSS. React Native defines flex as a number whereas the CSS for web defines it as a string.
Normally you will use flex: 1 , which tells a component to fill all available space, shared evenly amongst other components with the same parent. The larger the flex given, the higher the ratio of space a component will take compared to its siblings.
Justify Contentflex-end Align children of a container to the end of the container's main axis. center Align children of a container in the center of the container's main axis. space-between Evenly space off children across the container's main axis, distributing the remaining space between the children.
I also had this problem on my way to achieve the goals and I solved this like below.
Hope this could be helpful for the others who has similar case to save time.
https://github.com/brucelin0325/resizable_layout
MyComponent.js
import React, { Component } from 'react';
import { StyleSheet, View, Dimensions, PanResponder, Animated } from 'react-native';
export default class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
offset : 0,
topHeight : 40, // min height for top pane header
bottomHeight : 40, // min height for bottom pane header,
deviceHeight : Dimensions.get('window').height,
isDividerClicked: false,
pan : new Animated.ValueXY()
}
}
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
// Initially, set the Y position offset when touch start
onPanResponderGrant: (e, gestureState) => {
this.setState({
offset: e.nativeEvent.pageY,
isDividerClicked: true
})
},
// When we drag the divider, set the bottomHeight (component state) again.
onPanResponderMove: (e, gestureState) => {
this.setState({
bottomHeight : gestureState.moveY > (this.state.deviceHeight - 40) ? 40 : this.state.deviceHeight - gestureState.moveY,
offset: e.nativeEvent.pageY
})
},
onPanResponderRelease: (e, gestureState) => {
// Do something here for the touch end event
this.setState({
offset: e.nativeEvent.pageY,
isDividerClicked: false
})
}
});
}
render() {
return (
<View style={styles.content}>
{/* Top View */}
<Animated.View
style = {[{backgroundColor: 'pink', minHeight: 40, flex: 1}, {height: this.state.topHeight}]}
>
</Animated.View>
{/* Divider */}
<View
style={[{height: 10}, this.state.isDividerClicked ? {backgroundColor: '#666'} : {backgroundColor: '#e2e2e2'}]}
{...this._panResponder.panHandlers}
>
</View>
{/* Bottom View */}
<Animated.View
style={[{backgroundColor: 'green', minHeight: 40}, {height: this.state.bottomHeight}]}
>
</Animated.View>
</View>
)
}
}
const styles = StyleSheet.create({
content : {
flex : 1,
flexDirection: 'column'
},
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With