我已经创建了一个用getx flutter创建了一个自定义的下拉顿布顿,当我用obx包裹时,它会给我带来错误

发布于 2025-02-09 14:13:24 字数 4036 浏览 2 评论 0原文

自定义下拉列表

class CustomDropDown extends StatefulWidget {
PurchasePackageController purchasePackageController =
  PurchasePackageController();
final String hintName;
final List<String> listName;
final void Function(String?)? onChanged;
CustomDropDown(
  {Key? key,
  required this.hintName,
  required this.listName,
  required this.onChanged})
  : super(key: key);

@override
State<CustomDropDown> createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
String? selectedValue;

List<DropdownMenuItem<String>> buildDropdownMenuItems(List list) {
List<DropdownMenuItem<String>> dropDownItems = [];
list.forEach((value) {
  dropDownItems.add(DropdownMenuItem<String>(
    value: value,
    child: Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12.0),
      child: Text(
        value,
        style: TextStyle(fontSize: 16.0, color: colorPrimary),
      ),
    ),
  ));
});

return dropDownItems;
}

@override
Widget build(BuildContext context) {
return Padding(
  padding: EdgeInsets.symmetric(horizontal: 10.0.h),
  child: InputDecorator(
    decoration: InputDecoration(
      enabledBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: colorPrimary),
      ),
    ),
    isEmpty: true,
    //dropdownvalue == '',
    child: DropdownButtonHideUnderline(
      child: Obx(() => DropdownButton<String>(
            iconSize: 30.0.sp,
            iconEnabledColor: colorPrimary,
            hint: Padding(
              padding: EdgeInsets.symmetric(horizontal: 12.0.h),
              child: Text(
                widget.hintName,
                style: TextStyle(fontSize: 18.0.sp, color: colorPrimary),
              ),
            ),
            value: selectedValue,
            isDense: true,
            onChanged: (value) => widget.onChanged,
            buildDropdownMenuItems() 
            items: buildDropdownMenuItems(
              widget.listName,
            ),
          )),
    ),
  ),
);
}
}

视图

class LocationView extends GetView<LocationController> {


LocationView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: const Text('Location'),
      centerTitle: true,
    ),
    body: Column(
      children: [
        CustomDropDown(
          hintName: "Package Type",
          listName: controller.MDPackageType,
          onChanged: (newValue) =>
              controller.onChangedPackageName(newValue!),
        ),
        CustomDropDown(
            hintName: "Package Name",
            listName: controller.MDPackageType,
            onChanged: (newValue) =>
                controller.onChangedPackageType(newValue!)),
        Container(
          height: MediaQuery.of(context).size.height * 0.50,
          width: MediaQuery.of(context).size.width * 0.95,
          child: Column(children: [
            Text(controller.selectedPackageName.value),
            Text(controller.selectedPackageType.value)
          ]),
        )
      ],
    ));
}

}

控制器

class LocationController extends GetxController {


List<String> MDPackageType = [
'Package Type 1',
'Package Type 2',
'Package Type 3',
'Package Type 4',
];
List<String> MDPackageName = [
'Package Name 1',
'Package Name 2',
'Package Name 3',
'Package Name 4',
];
var selectedPackageType = "Package Type 1".obs;
var selectedPackageName = "Package Name 1".obs;

onChangedPackageType(String value) => selectedPackageType.value = value;
onChangedPackageName(String value) => selectedPackageName.value = value;
}

错误

投掷“” [GET]检测到GetX的不当使用。 您只能将GETX或OBX用于将要更新的特定小部件。 如果您看到此错误,您可能没有将任何可观察的变量插入GetX/obx 或将它们插入getx认为适合更新的范围之外 (示例:getx =&gt; heidewidget =&gt; variable observable)。 如果您需要更新父窗口小部件和子小部件,请将每个小部件包装在OBX/GETX中。 ”“”; }

并且,如果我删除了OBX,则可以代码正在运行,但我不能选择“ frrom”下拉菜单

Custom DropDownButton

class CustomDropDown extends StatefulWidget {
PurchasePackageController purchasePackageController =
  PurchasePackageController();
final String hintName;
final List<String> listName;
final void Function(String?)? onChanged;
CustomDropDown(
  {Key? key,
  required this.hintName,
  required this.listName,
  required this.onChanged})
  : super(key: key);

@override
State<CustomDropDown> createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
String? selectedValue;

List<DropdownMenuItem<String>> buildDropdownMenuItems(List list) {
List<DropdownMenuItem<String>> dropDownItems = [];
list.forEach((value) {
  dropDownItems.add(DropdownMenuItem<String>(
    value: value,
    child: Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12.0),
      child: Text(
        value,
        style: TextStyle(fontSize: 16.0, color: colorPrimary),
      ),
    ),
  ));
});

return dropDownItems;
}

@override
Widget build(BuildContext context) {
return Padding(
  padding: EdgeInsets.symmetric(horizontal: 10.0.h),
  child: InputDecorator(
    decoration: InputDecoration(
      enabledBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: colorPrimary),
      ),
    ),
    isEmpty: true,
    //dropdownvalue == '',
    child: DropdownButtonHideUnderline(
      child: Obx(() => DropdownButton<String>(
            iconSize: 30.0.sp,
            iconEnabledColor: colorPrimary,
            hint: Padding(
              padding: EdgeInsets.symmetric(horizontal: 12.0.h),
              child: Text(
                widget.hintName,
                style: TextStyle(fontSize: 18.0.sp, color: colorPrimary),
              ),
            ),
            value: selectedValue,
            isDense: true,
            onChanged: (value) => widget.onChanged,
            buildDropdownMenuItems() 
            items: buildDropdownMenuItems(
              widget.listName,
            ),
          )),
    ),
  ),
);
}
}

View

class LocationView extends GetView<LocationController> {


LocationView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: const Text('Location'),
      centerTitle: true,
    ),
    body: Column(
      children: [
        CustomDropDown(
          hintName: "Package Type",
          listName: controller.MDPackageType,
          onChanged: (newValue) =>
              controller.onChangedPackageName(newValue!),
        ),
        CustomDropDown(
            hintName: "Package Name",
            listName: controller.MDPackageType,
            onChanged: (newValue) =>
                controller.onChangedPackageType(newValue!)),
        Container(
          height: MediaQuery.of(context).size.height * 0.50,
          width: MediaQuery.of(context).size.width * 0.95,
          child: Column(children: [
            Text(controller.selectedPackageName.value),
            Text(controller.selectedPackageType.value)
          ]),
        )
      ],
    ));
}

}

Controller

class LocationController extends GetxController {


List<String> MDPackageType = [
'Package Type 1',
'Package Type 2',
'Package Type 3',
'Package Type 4',
];
List<String> MDPackageName = [
'Package Name 1',
'Package Name 2',
'Package Name 3',
'Package Name 4',
];
var selectedPackageType = "Package Type 1".obs;
var selectedPackageName = "Package Name 1".obs;

onChangedPackageType(String value) => selectedPackageType.value = value;
onChangedPackageName(String value) => selectedPackageName.value = value;
}

Error

throw """
[Get] the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
If you are seeing this error, you probably did not insert any observable variables into GetX/Obx
or insert them outside the scope that GetX considers suitable for an update
(example: GetX => HeavyWidget => variableObservable).
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.
""";
}

And if i remove obx, i can code is running but i cannot select items frrom the Drop Down Menu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-02-16 14:13:24

固定的!!!

必须更改

onChanged: (value) => widget.onChanged,

onChanged: (value) {
          setState(() {
            selectedValue = value;
          });
          if (widget.onChanged != null) widget.onChanged!(value);
        },

删除OBX

Fixed!!!

Had to change

onChanged: (value) => widget.onChanged,

to

onChanged: (value) {
          setState(() {
            selectedValue = value;
          });
          if (widget.onChanged != null) widget.onChanged!(value);
        },

And remove Obx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文