【问题标题】:How to create a Dropdown in Flutter from Firebase to save Document ID如何从 Firebase 在 Flutter 中创建下拉菜单以保存文档 ID
【发布时间】:2020-09-07 15:32:36
【问题描述】:

我正在尝试从 Flutter 中的 Firestore 创建一个下拉菜单。所以这就是我所拥有的:

所以我想做的是有一个 Select Dropdown 甚至是一个 搜索框显示文档中的字段 nombre,并根据所选名称将文档 ID 保存到变量中。

基本上下拉菜单应该显示为 Lable 的 Nombre 并保存 DocumentID 如果它可以是两者的组合(可搜索下拉菜单)会更好。

有什么想法吗?

亲切的问候。

【问题讨论】:

    标签: firebase flutter google-cloud-firestore drop-down-menu


    【解决方案1】:

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

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

    来源:How to bind a Firestore documents list to a Dropdown menu in Flutter?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 2020-02-04
      • 2016-11-07
      • 2016-06-29
      • 1970-01-01
      相关资源
      最近更新 更多