【问题标题】:Get the inputs from a ListView.Builder input fields? and pass as json从 ListView.Builder 输入字段获取输入?并作为json传递
【发布时间】:2021-02-02 20:28:33
【问题描述】:

我有这个看法。在 ModalBottomSheet 视图中的颤振应用程序中。

输入字段构建器

              Expanded(
                    flex: 6,
                    child: ListView.builder(
                      itemCount: 5,
                      itemBuilder: (BuildContext context, int index) {
                        return Container(
                          margin: EdgeInsets.symmetric(vertical: 4.0, horizontal: 4.0),
                          width: MediaQuery.of(context).size.width * 0.95,
                          // height: 100.0,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(
                                10.0,
                              )),
                              color: Color(0xFF01816B),
                              shape: BoxShape.rectangle),
                          //
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 4.0, vertical: 4.0),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "Category",
                                        style: kSubContentStyleWhiteLight,
                                      ),
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "Food Type",
                                        style: kSubContentStyleWhiteLight,
                                      ),
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "Quantity",
                                        style: kSubContentStyleWhiteLight,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 4.0, vertical: 4.0),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: Container(
                                        height: 48.0,
                                        padding: EdgeInsets.all(2.0),
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.all(Radius.circular(
                                              10.0,
                                            )),
                                            color: Color(0xFF03B898),
                                            shape: BoxShape.rectangle),
                                        child: DropdownButton<String>(
                                          value: _categorySelected,
                                          // icon: Icon(Icons.arrow_downward),
                                          hint: Text(
                                            'Category',
                                            style: kMainContentStyleWhite,
                                          ),
                                          iconSize: 24,
                                          elevation: 16,
                                          style: kMainContentStyleLightBlack,
                                          onChanged: (String newValue) {
                                            setState(() {
                                              _categorySelected = newValue;
                                            });
                                          },
                                          items: _categoryList
                                              .map<DropdownMenuItem<String>>(
                                                  (String value) {
                                            return DropdownMenuItem<String>(
                                              value: value,
                                              child: Text(value),
                                            );
                                          }).toList(),
                                        ),
                                      ),
                                    ),
                                    SizedBox(
                                      width: 8.0,
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: Container(
                                        height: 48.0,
                                        padding: EdgeInsets.all(2.0),
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.all(Radius.circular(
                                              10.0,
                                            )),
                                            color: Color(0xFF03B898),
                                            shape: BoxShape.rectangle),
                                        child: DropdownButton<String>(
                                          value: _foodTypeSelected,
                                          hint: Text(
                                            'FoodType',
                                            style: kMainContentStyleWhite,
                                          ),
                                          iconSize: 24,
                                          elevation: 2,
                                          style: kMainContentStyleLightBlack,
                                          onChanged: (String newValue) {
                                            setState(() {
                                              _foodTypeSelected = newValue;
                                            });
                                          },
                                          items: _foodTypeList
                                              .map<DropdownMenuItem<String>>(
                                                  (String value) {
                                            return DropdownMenuItem<String>(
                                              value: value,
                                              child: Text(value),
                                            );
                                          }).toList(),
                                        ),
                                      ),
                                    ),
                                    SizedBox(
                                      width: 8.0,
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: Container(
                                        height: 48.0,
                                        // color: Colors.grey[100],
                                        // padding: EdgeInsets.all(2.0),
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.all(Radius.circular(
                                              10.0,
                                            )),
                                            color: Color(0xFF03B898),
                                            shape: BoxShape.rectangle),
                                        alignment: Alignment.center,
                                        child: TextFormField(
                                          // controller: _mealPlanQtyController,
                                          inputFormatters: [
                                            new LengthLimitingTextInputFormatter(
                                                3),
                                          ],
                                          // decoration: InputDecoration(
                                          //   border: OutlineInputBorder(),
                                          // ),
                                          keyboardType: TextInputType.number,
                                          autofocus: false,
                                          validator: validateNumberDecimal,
                                          onSaved: (String value) {
                                            // _petWeight = value; //double.parse(value);
                                          },
//      initialValue: 'alucard@gmail.com',
                                          style: kMainContentStyleWhite,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),

在这里,我需要分别获取每个项目的每个值。(值来自两个下拉菜单和一个 TextInputField)。 我需要使用 API 以以下格式传递每个输入字段(有五个输入字段列表项)

{
  "user_email": "email@m.com",
  "user_token": "thisistoken",
  "feeding_plan_detail": {
    "quantity": "float",
    "food_id": "id",
    "feeding_plan_id": "id"
  }
}

我如何在颤振中做到这一点?

【问题讨论】:

    标签: api flutter listview dart


    【解决方案1】:

    创建一个模型类来存储输入值。

    class UserInputs {
        String category, foodType;
        double quantity;
        
        UserInputs({this.category = '', this.foodType = '', this.quantity = 0.0});
    }
    

    在您的 ModalBottomSheet 视图或输入字段构建器类中,使用默认值创建 UserInputs 列表并将每个输入值存储在此模型对象中。

    final _allUserInputs = List<UserInputs>[5];
    for (var i = 0; i < _allUserInputs.length; i++) {
        _allUserInputs = UserInputs(); // obj with default values
    }
    

    将用户输入的每个值保存在对象中,如下所示:

    Expanded(
          flex: 6,
          child: ListView.builder(
            itemCount: 5,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                margin: EdgeInsets.symmetric(vertical: 4.0, horizontal: 4.0),
                width: MediaQuery.of(context).size.width * 0.95,
                // height: 100.0,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(
                      10.0,
                    )),
                    color: Color(0xFF01816B),
                    shape: BoxShape.rectangle),
                //
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 4.0, vertical: 4.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Text(
                              "Category",
                              style: kSubContentStyleWhiteLight,
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: Text(
                              "Food Type",
                              style: kSubContentStyleWhiteLight,
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: Text(
                              "Quantity",
                              style: kSubContentStyleWhiteLight,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 4.0, vertical: 4.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              height: 48.0,
                              padding: EdgeInsets.all(2.0),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(
                                    10.0,
                                  )),
                                  color: Color(0xFF03B898),
                                  shape: BoxShape.rectangle),
                              child: DropdownButton<String>(
                                value: _categorySelected,
                                // icon: Icon(Icons.arrow_downward),
                                hint: Text(
                                  'Category',
                                  style: kMainContentStyleWhite,
                                ),
                                iconSize: 24,
                                elevation: 16,
                                style: kMainContentStyleLightBlack,
                                onChanged: (String newValue) {
                                  setState(() {
                                    _allUserInputs[index].category = newValue; // New
                                  });
                                },
                                items: _categoryList
                                    .map<DropdownMenuItem<String>>((String value) {
                                  return DropdownMenuItem<String>(
                                    value: value,
                                    child: Text(value),
                                  );
                                }).toList(),
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 8.0,
                          ),
                          Expanded(
                            flex: 1,
                            child: Container(
                              height: 48.0,
                              padding: EdgeInsets.all(2.0),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(
                                    10.0,
                                  )),
                                  color: Color(0xFF03B898),
                                  shape: BoxShape.rectangle),
                              child: DropdownButton<String>(
                                value: _foodTypeSelected,
                                hint: Text(
                                  'FoodType',
                                  style: kMainContentStyleWhite,
                                ),
                                iconSize: 24,
                                elevation: 2,
                                style: kMainContentStyleLightBlack,
                                onChanged: (String newValue) {
                                  setState(() {
                                    _allUserInputs[index].foodType = newValue; // New
                                  });
                                },
                                items: _foodTypeList
                                    .map<DropdownMenuItem<String>>((String value) {
                                  return DropdownMenuItem<String>(
                                    value: value,
                                    child: Text(value),
                                  );
                                }).toList(),
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 8.0,
                          ),
                          Expanded(
                            flex: 1,
                            child: Container(
                              height: 48.0,
                              // color: Colors.grey[100],
                              // padding: EdgeInsets.all(2.0),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(
                                    10.0,
                                  )),
                                  color: Color(0xFF03B898),
                                  shape: BoxShape.rectangle),
                              alignment: Alignment.center,
                              child: TextFormField(
                                // controller: _mealPlanQtyController,
                                inputFormatters: [
                                  new LengthLimitingTextInputFormatter(3),
                                ],
                                // decoration: InputDecoration(
                                //   border: OutlineInputBorder(),
                                // ),
                                keyboardType: TextInputType.number,
                                autofocus: false,
                                validator: validateNumberDecimal,
                                onSaved: (String value) {
                                  _allUserInputs[index].quantity = double.parse(value); // New
                                  // _petWeight = value; //double.parse(value);
                                },
                                //      initialValue: 'alucard@gmail.com',
                                style: kMainContentStyleWhite,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ),
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多