Flutter - allow user enter hashtags

青春壹個敷衍的年華 提交于 2020-12-26 11:21:33

问题


Hello Flutter newbie here! I want to let my users enter some hashtags linked to the entry, which will go into 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. On Firestore I have set the field to be an array to receive the data.

I can't find much documentation on creating hashtag on flutter. Any help would be appreciated!! :) Thanks in advance!


回答1:


Being I used dartpad to create this, I used ListView for suggestions. You may replace with your own suggestions view like AutoCompleteTextView or something else...

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;
                })
          ]),
    );
  }



来源:https://stackoverflow.com/questions/60476222/flutter-allow-user-enter-hashtags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!