简体   繁体   English

如何在 flutter 中使用 Getx 制作下拉按钮?

[英]How can I make dropdownbutton using Getx in flutter?

I'm trying to make dropdownbutton using Getx in flutter However, it doesn't work.我正在尝试在 flutter 中使用 Getx 制作下拉按钮但是,它不起作用。 Even if I choose a value, the value does not been selected.即使我选择了一个值,该值也没有被选择。


class BecomePlayerPage2 extends GetView<BecomePlayerController> {
  const BecomePlayerPage2 ({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(
          () => Scaffold(
        body: Padding(
          padding: EdgeInsets.all(20),
          child:
              DropdownButton<RxString>(
                onChanged: (newValue){
                  controller.selected=newValue!;
                },
                value: controller.selected,
                items: [
                  for(var value in controller.tierList)
                    DropdownMenuItem(
                        child: new Text(
                          value,
                        ),
                    value: value.obs,
                    ),
                ]

              ),
          ),
        ),   
}

class BecomePlayerController extends GetxController {

  final tierList=['first', 'second', 'third'].obs;
  var selected = "first".obs;



}

This is my first day of studying Getx so it is so hard to match with basic widget.这是我学习 Getx 的第一天,所以很难与基本的 widget 匹配。 What should I do?我应该怎么办?

Use round brackets to update.obs and change RxString to String使用圆括号update.obs,将RxString改为String

authController.selected(newValue.toString());
 onChanged: (newValue){
    // controller.selected=newValue!; 
    controller.selected.value =newValue!;
 },

Try replacing the onChange statement with尝试将 onChange 语句替换为

onChanged: (newValue) {
   controller.selected.value = newValue.toString();
},

or by changing Dropdown button type from RXString to String或者通过将下拉按钮类型从RXStringString

return Obx(
  () => Scaffold(
    body: Padding(
      padding: const EdgeInsets.all(20),
      child: DropdownButton<String>( // updated
          onChanged: (newValue) {
            controller.selected.value = newValue!; //updated
          },
          value: controller.selected.value, //updated
          items: [
            for (var value in controller.tierList)
              DropdownMenuItem(
                value: value, 
                child: Text(
                  value, //updated
                ),
              ),
          ]),
    ),
  ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中使用 Getx 实现 DropdownButton - How do implement a DropdownButton using Getx in Flutter Flutter 带有 Getx 的 DropdownButton 小部件 - Flutter DropdownButton widget with Getx Flutter:如何使用 Getx 更改变量? - Flutter : How can I change variable with Getx? 如何在 GetX Flutter 中获取 BuildContext? - How can I get BuildContext in GetX Flutter? 在 Flutter 中,我可以使 DropdownButton 的子项成为 DropdownMenuItem 以外的小部件吗 - In Flutter can I make the children of a DropdownButton a widget other than DropdownMenuItem Flutter:当列表数据更改时,getx controller 未更新。 我如何确保添加的每个列表,GetX controller 知道这一点? - Flutter: getx controller not updated when list data changed. How can i make sure with every list added, GetX controller knows that? 如何在 DropDownButton 中显示列或容器? flutter - How can i show a column or container inside DropDownButton? flutter 如何更改 DropdownButton 中 Flutter DropdownMenuItem 的宽度/填充? - How can i change the width/padding of a Flutter DropdownMenuItem in a DropdownButton? 如何从 Flutter 中的另一个小部件更改 DropdownButton 值? - How can I change DropdownButton values from another widget in Flutter? 我如何在 dropDownButton 中显示我的数组,flutter? - How i can display my array in dropDownButton , flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM