簡體   English   中英

從 flutter 中的下拉按鈕獲取值后如何點擊並獲取數據

[英]how to hit and get data after getting value from dropdownbutton in flutter

我需要知道如何在從 dropdownButton 中選擇一個值后點擊 API 以及如何在 flutter 中的文本中使用該響應。在這里我可以點擊 api 並得到響應但它使用的是 null 或值沒有改變。 我將分享圖像和代碼。

Dropdown
 Widget partNameDropdown() {
    return SizedBox(
      width: 170,
      height: 50,
      child: DropdownButton<LinishingMaster_Data>(
        borderRadius: BorderRadius.circular(10.0),
        underline: SizedBox(),
        focusColor: AppColors().linenDark,
        elevation: 1,
        enableFeedback: true,
        isExpanded: true,
        style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
            fontWeight: FontWeight.w400),
        icon: Icon(
          Icons.keyboard_arrow_down_rounded,
          color: AppColors().linenMedium,
        ),
        items: partName!.map((LinishingMaster_Data item) {
          return DropdownMenuItem<LinishingMaster_Data>(
            child: Text('${item.description.toString()}'),
            value: item,
          );
        }).toList(),
        onChanged: (value) {
          setState(() {
            selectedPartName = value as LinishingMaster_Data;

            entity = getLotNo(selectedPartName!.sId) as LinishingLotNo?;
            seq ? isSelected = true : isSelected = false;
          });
        },
        value: selectedPartName,
        hint: Text("Select item"),
      ),
    );
  }
API
Future<LinishingLotNo> getLotNo(id) async {
    try {
      var res = await repository.linishing_LotNo(id);
      //await getDataUploaded();
      entity = await res;
    } catch (e) {
      print(e);
    }
    seq = true;
    lastLotNo = entity!.last_lot_number;
    print(entity);
    return entity!;
  }
 Widget getAdditionalField() {
    return Column(
      children: [
        SizedBox(
          height: 8,
          width: 80,
          child: TextFormField(
            controller: batchController,
            keyboardType: TextInputType.number,
            validator: (val) {
              if (val!.isEmpty || !RegExp(r'^[0-9]+$').hasMatch(val)) {
                return '';
              } else {
                return null;
              }
            },
          ),
        ),
        SizedBox(
          height: AppSizes().hspacer4 * 1.1,
        ),
        Text(
          lastLotNo ?? "",
          style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
          ),
        ),
        SizedBox(
          height: AppSizes().hspacer4,
        ),
        Wrap(
          spacing: 8.0,
          children: List.generate(entity!.lot_suggestion!.length, (index) {
            return InkWell(
              onTap: () {
                setState(() {
                  selectedLotIndex = index;
                  selectedLotValue = entity!.lot_suggestion![index].value;
                  print('----------->LOtVALUE$selectedLotValue');
                });
              },
              child: LotButtons(
                colors: selectedLotIndex == index
                    ? Colors.black54
                    : Colors.grey.shade50,
                borderColor: Colors.black54,
                text: entity!.lot_suggestion![index].display.toString(),
                backgroundColor:
                    selectedLotIndex == index ? Colors.white : Colors.black54,
              ),
            );
          }),
        ),
        SizedBox(
          height: AppSizes().hspacer1,
        ),
        Wrap(
          spacing: 8.0,
          children: List.generate(3, (index) {
            return InkWell(
              onTap: () {
                setState(() {
                  selectedIndex = index;
                  selectedShift = index + 1;

                  print('---------------->>>>>${index + 1}');
                });
              },
              child: ShiftButtons(
                colors: selectedIndex == index
                    ? Colors.black54
                    : Colors.grey.shade50,
                borderColor: Colors.black54,
                text: (index + 1).toString(),
                backgroundColor:
                    selectedIndex == index ? Colors.white : Colors.black54,
              ),
            );
          }),
        ),
      ],
    );
  }
Text to display data which I will get from API call
Widget getSideUiFromBack() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '$empId - $empName',
          style: GoogleFonts.montserrat(
            fontSize: AppSizes().paraLarge16,
            color: AppColors().linenDark,
          ),
          overflow: TextOverflow.clip,
        ),
        SizedBox(
          height: AppSizes().hspacer2,
        ),
        machineDropdown(),
        SizedBox(
          height: AppSizes().hspacer1,
        ),
        partNameDropdown(),// This Dropdown value
        SizedBox(
          height: AppSizes().hspacer2,
        ),
        isSelected ? getAdditionalField() : Container(), // Here it is 
      ],
    );
  }

..................謝謝.............................. …………

如果沒有一些我們可以運行的代碼,很難為您提供幫助,但可以肯定的是,在下拉列表的onChanged部分中分配數據的方式存在問題:

    onChanged: (value) async {
      YourDataModel data = await getLotNo(selectedPartName!.sId);
      setState(() {
        selectedPartName = value as LinishingMaster_Data;

        entity =  data as LinishingLotNo?;
        seq ? isSelected = true : isSelected = false;
      });
    },

在這里您需要await getLotNo API 調用的結果,然后在您的setState中將該結果分配給任何保持其值的變量

您還需要修復 API 調用以實際返回一些數據而不是將其設置為變量:

Future<LinishingLotNo> getLotNo(id) async {
    try {
      var res = await repository.linishing_LotNo(id);
      //await getDataUploaded();
    } catch (e) {
      print(e);
    }
    seq = true;
    lastLotNo = res!.last_lot_number;
    print(res);
    return res!;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM