react-native-picker / picker

Picker is a cross-platform UI component for selecting an item from a list of options.
MIT License
1.51k stars 289 forks source link

Can't place the Picker on beside text when 'flexDirection' is row. #52

Open lightning331 opened 4 years ago

lightning331 commented 4 years ago

My example code:

...
  render() {
    return (
      <View style={styles.container}>
        <Text>Height</Text>
        <Text style={styles.result_text}>170</Text>
        <Picker
          selectedValue={this.state.unit}
          style={{height: 50, width: 100}}
          itemStyle={{textAlign: 'left', fontSize: 14}}
          onValueChange={(itemValue, itemIndex) =>
            this.setState({unit: itemValue})
          }>
          <Picker.Item label="cm" value="cm" />
          <Picker.Item label="feet, inches" value="feet" />
        </Picker>
      </View>
    );
...
const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
  },
}
hakkikonu commented 4 years ago

try to wrap <Picker> component with <View> component

jainkuniya commented 4 years ago

Can you try this

 <View
        style={{
          flexDirection: "row",
          alignItems: "center",
        }}
      >
        <Text>Language:</Text>
        <PickerIOS
          style={{ flex: 1 }}
          mode="dialog"
          itemStyle={{ fontWeight: "bold", color: "#ff0000" }}
          selectedValue={this.state.language}
          onValueChange={(itemValue, itemIndex) =>
            this.setState({ language: itemValue })
          }
        >
          <PickerIOS.Item label="Java" value="java" />
          <PickerIOS.Item label="JavaScript" value="js" />
        </PickerIOS>
      </View>

image

x-ji commented 2 years ago

Is there a solution for Android?

I tried

const Task = ({route}) => (
  <ScrollView>
    <View
      style={tw`flex flex-row justify-between items-center bg-white border border-gray-200`}
    >
      <Text>Task state</Text>
      <Picker>
        <Picker.Item label="NEXT" value="NEXT" />
        <Picker.Item label="INPROGRESS" value="INPROGRESS" />
      </Picker>
    </View>
  </ScrollView>
);

and launched the app in Android, which looks like this. But the problem is that clicking on the picker results in nothing, i.e. the selections don't show up.

Screen Shot 2022-01-23 at 12 05 28
x-ji commented 2 years ago

I made it work according to this: https://stackoverflow.com/a/42851316/1460448. Setting flex:.5 on both the Text and the Picker was the key.