简体   繁体   English

如何从子小部件更新父小部件的状态并将其反映在子小部件中?

[英]How to update state of parent widget from child widget and reflect it in child widget?

Hello I have parent stateful widget which has button on tap of which I show a bottom sheet in which I have dropdown menu.你好,我有父状态小部件,它有一个点击按钮,我显示一个底部工作表,其中有下拉菜单。 now the state variable, list and function all are in the parent widget when choose any option in drop down it call the function to set that variable to new value but it doesn't reflect on the screen.现在状态变量、列表和函数都在父小部件中,当在下拉列表中选择任何选项时,它会调用函数将该变量设置为新值,但它不会反映在屏幕上。 when pop the bottom sheet and open it again it shows the updated value.pop底部工作表并再次打开它时,它会显示updated值。

parent,家长,

class PageAssets extends StatefulWidget {
  @override
  _PageAssetsState createState() => _PageAssetsState();
}

class _PageAssetsState extends State<PageAssets> {
OrganisationModel selectedOrganisation; //-----> state variable 
 List<OrganisationModel> lstOrganisation = []; //----> list of values

selectedOrgChanged(OrganisationModel plant) { //----> function which is to call on change 
    if (mounted) {
      setState(() {
        selectedOrganisation = plant;
      });
    }
    loadPlants(plant: selectedOrganisation);
  }

//somewhere in UI
...
IconButton(icon: Icon(Icons.tune),
              onPressed: () async {
                  showModalBottomSheet();
          })

}

custom drop down button自定义下拉按钮

FilterDropDown(
                      title: 'Organization',
                      onOrgChange: (val) {
                        widget.selectedOrgChanged(val);
                      },
                      orgList: widget.lstOrganisation,
                      selectedOrg: widget.selectedOrganisation,
                    )

custom dropdown menu自定义下拉菜单

class FilterDropDown extends StatelessWidget {
  final OrgBase selectedOrg;
  final List<OrgBase> orgList;
  final OnOrgChange onOrgChange;
  final String title;

  FilterDropDown({
    this.title,
    this.orgList,
    this.selectedOrg,
    this.onOrgChange,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Row(
      children: [
        SizedBox(
          width: 10,
        ),
        SizedBox(
          width: 130,
          child: Text(
            title,
            style: theme.textTheme.subtitle2,
          ),
        ),
        Container(
          width: 150,
          height: 55,
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
          padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),
          decoration: BoxDecoration(
              color: Color(0xffe4e4eb),
              borderRadius: BorderRadius.circular(10)),
          child: DropdownButton<OrgBase>(
              isExpanded: true,
              icon: Icon(Icons.arrow_drop_down),
              iconSize: 30,
              hint: Text('--Choose--'),
              underline: SizedBox(),
              value: selectedOrg,
              items: orgList
                  .map<DropdownMenuItem<OrgBase>>(
                      (value) => DropdownMenuItem<OrgBase>(
                          value: value,
                          child: Text(
                            value.label,
                            style: theme.textTheme.subtitle1,
                          )))
                  .toList(),
              onChanged: onOrgChange),
        ),
      ],
    );
  }
}

so how can I fix this?那么我该如何解决这个问题?

inside FilterDropDown on DropdownButton ,DropdownButton FilterDropDown内,

replace onChanged: onOrgChange, with onChanged: (value) => onOrgChange(value),替换onChanged: onOrgChange,onChanged: (value) => onOrgChange(value),

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

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