如何从Firebase在Flutter中创建下拉菜单以保存文档ID

雷内·阿拉斯(Rene Alas)

我正在尝试从Flutter中的Firestore创建一个Dropdown。这就是我所拥有的:

Firestore收集信息

所以我想这样做是有一个选择下拉,甚至一个搜索框,显示领域NOMBRE从文档,并根据所选择的名称保存文档ID到一个变量。

基本上,Dropdown应该显示为Lable's Nombre并保存DocumentID(如果可以将两者结合使用)(可搜索Dropdown)会更好。

有任何想法吗?

亲切的问候。

maniSidhu98

过去已经回答了这个问题。

    new StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance.collection('categories').snapshots(),
        builder: (context, snapshot){
          if (!snapshot.hasData) return const Center(
            child: const CupertinoActivityIndicator(),
          );
          var length = snapshot.data.documents.length;
          DocumentSnapshot ds = snapshot.data.documents[length - 1];
          _queryCat = snapshot.data.documents;
          return new Container(
            padding: EdgeInsets.only(bottom: 16.0),
            width: screenSize.width*0.9,
            child: new Row(
              children: <Widget>[
                new Expanded(
                    flex: 2,
                    child: new Container(
                      padding: EdgeInsets.fromLTRB(12.0,10.0,10.0,10.0),
                      child: new Text("Category",style: textStyleBlueBold,),
                    )
                ),
                new Expanded(
                  flex: 4,
                  child:new InputDecorator(
                    decoration: const InputDecoration(
                      //labelText: 'Activity',
                      hintText: 'Choose an category',
                      hintStyle: TextStyle(
                        color: primaryColor,
                        fontSize: 16.0,
                        fontFamily: "OpenSans",
                        fontWeight: FontWeight.normal,
                      ),
                    ),
                    isEmpty: _category == null,
                    child: new DropdownButton(
                      value: _category,
                      isDense: true,
                      onChanged: (String newValue) {
                        setState(() {
                          _category = newValue;
                          dropDown = false;
                          print(_category);
                        });
                      },
                      items: snapshot.data.documents.map((DocumentSnapshot document) {
                        return new DropdownMenuItem<String>(
                            value: document.data['title'],
                            child: new Container(
                              decoration: new BoxDecoration(
                                  color: primaryColor,
                                  borderRadius: new BorderRadius.circular(5.0)
                              ),
                              height: 100.0,
                              padding: EdgeInsets.fromLTRB(10.0, 2.0, 10.0, 0.0),
                              //color: primaryColor,
                              child: new Text(document.data['title'],style: textStyle),
                            )
                        );
                      }).toList(),
                    ),
                  ),
                ),
              ],
            ),
          );
        }
    );

来源:如何将Firestore文档列表绑定到Flutter中的下拉菜单?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章