Flutter - allow user enter hashtags

后端 未结 1 1550
一整个雨季
一整个雨季 2021-01-24 23:57

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 no

相关标签:
1条回答
  • 2021-01-25 00:22

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

    0 讨论(0)
提交回复
热议问题