简体   繁体   English

Flutter 中的自定义下拉小部件如何实现 setState

[英]Custom dropdown widget in Flutter how to implement setState

I implemented a custom DropdownButton widget, but I don't know how to implement it's setState.我实现了一个自定义 DropdownButton 小部件,但我不知道如何实现它的 setState。 I would like to pass items and selectedItem to the widget, and let it to handle it's own state.我想将项目和 selectedItem 传递给小部件,并让它处理它自己的 state。 And retrieve selected item when needed by myDropdownButton.selectedItem.并在 myDropdownButton.selectedItem 需要时检索所选项目。 How I could implement it?我怎么能实现它?

class MyDropdownButton extends StatefulWidget {
  final String selected;
  final List<MyDropdownItem> items;

  MyDropdownButton({Key key, this.selected, this.items})
      : super(key: key);

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

class _MyDropdownButtonState extends State<MyDropdownButton> {
  Widget build(BuildContext context) {
  return DropdownButtonFormField(
  value: widget.selected,
  onChanged: (String value) {
            widget.selected = value;
            },

But the selected is final and cannot be modified.但是选择的是最终的,不能修改。 How to implement it?如何实施?

Thank you!谢谢!

There are two questions here:这里有两个问题:

  1. Updating the widget, using setState.使用 setState 更新小部件。

  2. Passing the value back to the widget that is using the Dropdown with a callback.通过回调将值传递回使用 Dropdown 的小部件。 Medium Article on callbacks 关于回调的中型文章

Firstly to have the dropdown update, you need to call a setstate on the value change.首先要更新下拉列表,您需要在值更改时调用 setstate。 But first, you'll need to receive the value passed, usually this is done in initstate.但首先,您需要接收传递的值,通常这是在 initstate 中完成的。

Second, you need to use a callback function.其次,需要使用回调 function。 The class that calls this widget/class can then receive and process that value调用此小部件/类的 class 然后可以接收和处理该值

class MyDropdownButton extends StatefulWidget {
  final String selected;
  final List<MyDropdownItem> items;
  
  final Function(String) valueReturned; //callback function

  MyDropdownButton({Key key, this.selected, this.items, this.valueReturned})
      : super(key: key);

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

class _MyDropdownButtonState extends State<MyDropdownButton> {
  String sel;
  @override
  void initState() { 
    super.initState();
    sel = widget.selected; //get the value passed
  }
  Widget build(BuildContext context) {
  return DropdownButtonFormField(
  value: sel
  onChanged: (String value) {
    setState() {
            sel = value;
            widget.valueReturned(value); //this will trigger the callback function
            },
      }

In the code that calls the widget, you will need to listen to and handle the response.在调用小部件的代码中,您需要监听并处理响应。

Container(
  child: MyDropdownButton(items: items, selected: selected, valueReturned: _handleValueReturned))

_handleValueReturned(String value) {
    thingToUpdate = value;
}     

Define a local variable and initialize it in initState():定义一个局部变量并在 initState() 中对其进行初始化:

String _selected;
@override
void initState() { 
  super.initState();
  _selected = widget.selected; 
}

use setState to update your local variable as the selection changes:使用 setState 在选择更改时更新您的局部变量:

onChanged: (String value) {
  setState(() {_selected = value;})
}

To retrieve the value, define a getter in your class:要检索该值,请在 class 中定义一个 getter:

String get selectedItem => _selected;

You can then access the selected item using myDropdownButton.selectedItem .然后,您可以使用myDropdownButton.selectedItem访问所选项目。

For more detailed explanation on implicit and explicit getters/setters see How do getters and setters change properties in Dart?有关隐式和显式 getter/setter 的更详细说明,请参阅getter 和 setter 如何更改 Dart 中的属性?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM