【发布时间】:2018-05-28 12:07:02
【问题描述】:
我正在尝试使用来自 ui kit nachos-ui 的平面列表和消息气泡样式创建消息传递界面,但我无法让平面列表根据文本量呈现不同宽度的文本气泡。每当我第一次发送消息时,气泡的宽度似乎都不错:
但是,每当我发送另一条消息时,虽然在这些图片中很难看到,但它会更改前一条消息的宽度并将新消息限制为似乎是最大宽度
这是我的平面列表代码:
<FlatList
data={this.state.messages}
style={{marginLeft: 280}}
ref={ref => this.flatList = ref}
onContentSizeChange={() => this.flatList.scrollToEnd({animated: true})}
onLayout={() => this.flatList.scrollToEnd({animated:true})}
keyExtractor = {item => item.timestamp}
renderItem={({item}) => <Bubble style={{marginTop: 20}} color="#FFC800"
>{item.contents}</Bubble>}
/>
整个组件的代码也可能有助于诊断问题:
<KeyboardAvoidingView style={styles.container}
behavior="padding"
keyboardVerticalOffset={64}>
<KeyboardAvoidingView style={styles.inputContainer}>
<FlatList
data={this.state.messages}
style={{marginLeft: 280}}
ref={ref => this.flatList = ref}
onContentSizeChange={() => this.flatList.scrollToEnd({animated: true})}
onLayout={() => this.flatList.scrollToEnd({animated:true})}
keyExtractor = {item => item.timestamp}
renderItem={({item}) => <Bubble style={{marginTop: 20}} color="#FFC800"
>{item.contents}</Bubble>}
/>
<View style={{flexDirection:'row', backgroundColor: 'transparent'}}>
<Input
containerStyle={{marginVertical: 10, width:300, marginLeft: 20}}
inputStyle={styles.textInput}
keyboardAppearance="dark"
placeholder=""
autoCorrect={false}
onChangeText={(message) => { this.setState({message})}}
value={this.state.message}
/>
<Button
buttonStyle={{borderRadius: 25, marginTop: 40, marginLeft: 10, paddingVertical: 5, backgroundColor: "#9214FF"}}
icon={{name: 'arrow-up', type: 'feather', color:'white'}}
onPress={()=>{Keyboard.dismiss; this.onPressButton()}}
title=""/>
</View>
</KeyboardAvoidingView>
</KeyboardAvoidingView>
如何让平面列表根据提交的文本量以动态大小呈现每条消息,而不更改任何先前的消息,并且没有预先确定的最大宽度?
【问题讨论】:
标签: javascript css react-native react-native-flatlist