flutter - Flutter Textfield 从选项列表中选择
问题描述
我的 Flutter 应用程序中有一个消息组件。当用户发送新消息时,我希望用户只能从选项列表中选择收件人(即他们只能发送给有效的用户名)。
视图看起来像这样
TextField(
cursorColor: etenRed,
controller: recipient,
decoration: InputDecoration(
hintText: 'To: Username',
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: etenRed,
),
),
),
onChanged: (text) {
model.onSearchChanged(text);
},
),
model.isSearchingForUsers
? SizedBox(
height: 150,
child: ListView.separated(
separatorBuilder: (context, index) {
return Divider(
thickness: 0.5,
color: Colors.black54,
);
},
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: model.snapshots.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () {
recipient.text = model
.snapshots[index].data['username'];
model.resetList();
},
title: Text(model
.snapshots[index].data['username']),
);
}),
)
: Container(),
如何确保用户无法输入不存在的用户名?
解决方案
尝试这样的事情:
if(usernames.contains(model.search)) {
sendMessage();
}
usernames
是用户名列表,model.search 是用户输入的用户名
推荐阅读
- xamarin - XAMARIN Visual Studio 2017 - Android 模拟器黑屏
- telegram - 如何通过 TDLib(电报数据库库)禁止组中的用户
- javascript - 在 JS 中使用 twig 变量
- kubernetes - Kubernetes 容器中 JVM 的运行时处理器计算
- javascript - 尝试从 html 读取数据时出现 w.fn.init 错误
- javascript - 使用 react-native-svg-charts 以日期为 x 轴的图表
- python - 按其在python中的元素排序列表(多维)
- python - 序列化器 DRF 中数据的非模型字段
- android - 具有原生反应的 Android 和 CouchDB
- apache-spark - 使用 Spark SQL JDBC 服务器/直线或 spark-sql