0

我创建了一个名为的文本字段chargesMax 并添加了一个ListTile包含复选框和两个名为chargesand的文本字段的小部件time。我想当用户在文本字段上输入一些值然后chargesMax检查任何文本字段应该包含收费文本字段的值。然后如果用户修改然后对文本字段收费,那么只有这个字段会修改而不是所有的文本字段。checkboxchargeslistTile

这是我的代码

charges.text = chargesMax.text;

在上面的这一行中,我正在尝试做我想做的事情,但是当我选中任何 listTile 的复选框时,chargesMax 的值会显示为收费文本字段,但复选框的所有文本字段都会在选中任何复选框时进行修改。然而,我可以单独检查复选框。

完整代码:


TextEditingController time = TextEditingController();
TextEditingController charges = TextEditingController();
TextEditingController chargesMax = TextEditingController();
List _selectedCities = [];

void _onCitySelected(bool selected, cityId) {
    if (selected == true) {
      setState(() {
        charges.text = chargesMax.text;  
        _selectedCities.add(cityId);
        print(_selectedCities);
      });
    } else {
      setState(() {
        _selectedCities.remove(cityId);
      });
    }
  }
Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
        child: Column(
              children: [
                    //chargesMax textfield
                    textformfieldCustomwithouticon(
                    context,
                    TextInputType.number,
                    MediaQuery.of(context).size.width * 0.9,
                    chargesMax, (String value) {
                  setState(() {
                    chargesMax.text = value;

                    chargesMax.selection = TextSelection.fromPosition(
                        TextPosition(offset: chargesMax.text.length));
                  });
                }, (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter some text';
                  }
                  return null;
                }, 'Enter delivery charges for maximumn destination',
                    'Delivery charges', 55.0),
                
                   SizedBox(
                                height:
                                    MediaQuery.of(context).size.height * 0.6,
                                child: ListView.builder(
                                    itemCount: books.length,
                                    itemBuilder: (context, index) {
                                      final book = books[index];
                                      if (isLoading) {
                                        return showCircularLoader(context);
                                      } else {
                                        return Padding(
                                          padding: const EdgeInsets.fromLTRB(
                                              0, 10, 0, 0),
                                          child: Column(
                                            children: [
                                              buildCitesList(book),
                                              SizedBox10(),
                                              Divider(),
                                              SizedBox10()
                                            ],
                                          ),
                                        );

                                        //
                                      }
                                    }),
                              ),


Widget buildCitesList(Book book) => ListTile(
        leading: checkboxCustom(context, _selectedCities.contains(book.id),
            (bool? selected) {
          if (selected != null) {
            setState(() {
              _onCitySelected(selected, book.id);
            });
          }
        }),
        title: Text(book.title, style: GoogleFonts.montserrat()),
        subtitle: Padding(
          padding: const EdgeInsets.only(top: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
             
              //charges textfield
              textformfieldCustomwithouticon1(
                  context,
                  TextInputType.number,
                  MediaQuery.of(context).size.width * 0.3,
                  charges, (String value) {
                setState(() {
                  charges.text = value;
                  charges.selection = TextSelection.fromPosition(
                      TextPosition(offset: charges.text.length));
                });
              }, (value) {
                if (value == null || value.isEmpty) {
                  return 'Please enter some text';
                }
                return null;
              }, 'Charges', 'Charges', 10.0),
              SizedBox(
                width: 10,
              ),
              textformfieldCustomwithouticon1(
                  context,
                  TextInputType.number,
                  MediaQuery.of(context).size.width * 0.3,
                  time, (String value) {
                setState(() {
                  time.text = value;
                  time.selection = TextSelection.fromPosition(
                      TextPosition(offset: time.text.length));
                });
              }, (value) {
                if (value == null || value.isEmpty) {
                  return 'Please enter some text';
                }
                return null;
              }, 'Time', 'Time', 10.0),
              // SizedBox10(),
            ],
          ),
        ),
      );

这是输出的快照

在此处输入图像描述

请帮助我如何做到这一点。

4

1 回答 1

0

.addListenerchargesMaxControllerchargesMax. _

这是您所需输出的简化版本。


class TimeToCode extends StatefulWidget {
  TimeToCode({Key? key}) : super(key: key);

  @override
  _TimeToCodeState createState() => _TimeToCodeState();
}

class _TimeToCodeState extends State<TimeToCode> {
  TextEditingController chargesMaxController = TextEditingController();

  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();

  bool tile1CheckBox = false;
  bool tile2CheckBox = false;

  @override
  void initState() {
    super.initState();

    chargesMaxController.addListener(() {
      if (tile1CheckBox) {
        setState(() {
          controller1.text = chargesMaxController.text;
        });
      }
      if (tile2CheckBox) {
        setState(() {
          controller2.text = chargesMaxController.text;
        });
      }
    });
  }
 @override
  void dispose() {
    chargesMaxController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Text("chargesMax "),
          TextField(
            controller: chargesMaxController,
          ),
          ListTile(
            leading: Checkbox(
              value: tile1CheckBox,
              onChanged: (value) {
                setState(() {
                  tile1CheckBox = value!;
                });
                if (value!) {
                  setState(() {
                    controller1.text = chargesMaxController
                        .text; // handle Ui update on checkedBOx value changes
                  });
                }
              },
            ),
            title: TextField(
              controller: controller1,
            ),
          ),
          ListTile(
            leading: Checkbox(
              value: tile2CheckBox,
              onChanged: (value) {
                setState(() {
                  tile2CheckBox = value!;
                });
                if (value!) {
                  setState(() {
                    controller2.text = chargesMaxController
                        .text; // handle Ui update on checkedBOx value changes
                  });
                }
              },
            ),
            title: TextField(
              controller: controller2,
            ),
          )
        ],
      ),
    );
  }
}
于 2021-11-21T08:02:30.813 回答