Flutter - 允许用户输入主题标签

2023-12-01

你好,Flutter 新手!我想让我的用户输入一些链接到该条目的主题标签,这些标签将进入 Firestore。

For the hashtag, I set it as a List but I'm not sure how to let user create the hashtags? Basically something like the tags field in SO's ask a question. stackoverflow hashtag On Firestore I have set the field to be an array to receive the data.

我找不到太多关于在 flutter 上创建主题标签的文档。 任何帮助,将不胜感激!! :) 提前致谢!


由于我使用 dartpad 来创建它,因此我使用 ListView 来获取建议。 您可以替换为您自己的建议视图,例如 AutoCompleteTextView 或其他内容......

List<String> list = ['Java', 'Flutter', 'Kotlin', 'Swift', 'Objective-C'],
      selected = [];
  TextEditingController tc;

  @override
  void initState() {
    super.initState();
    tc = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Search Tags'),
        backgroundColor: Colors.green[800],
      ),
      body: Column(
//         mainAxisSize:MainAxisSize.min,
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(
                  controller: tc,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      contentPadding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                      prefixIcon: selected.length < 1
                          ? null
                          : Padding(
                            padding: const EdgeInsets.only(left:10, right:10),
                            child: Wrap(
                                spacing: 5,
                                runSpacing: 5,
                                children: selected.map((s) {
                                  return Chip(
                                      backgroundColor: Colors.blue[100],
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(7),
                                      ),
                                      label: Text(s,
                                          style:
                                              TextStyle(color: Colors.blue[900])),
                                      onDeleted: () {
                                        setState(() {
                                          selected.remove(s);
                                        });
                                      });
                                }).toList()),
                          ))),
            ),
            SizedBox(height: 20),
            ListView.builder(
                shrinkWrap: true,
                itemCount: list.length,
                itemBuilder: (c, i) {
                  return list[i].toLowerCase().contains(tc.text.toLowerCase())
                      ? ListTile(
                          title: Text(list[i],
                              style: TextStyle(color: Colors.blue[900])),
                          onTap: () {
                            setState(() {
                              if (!selected.contains(list[i]))
                                selected.add(list[i]);
                            });
                          })
                      : null;
                })
          ]),
    );
  }

Screenshot

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter - 允许用户输入主题标签 的相关文章

随机推荐