使用多个小部件时如何在下拉按钮上更新文本

时间:2019-06-07 07:34:36

标签: flutter

我从json获取项目并将其显示在下拉列表中,当该人从下拉列表中选择一项时,当我使用body = _buildContent()时,所选项目会发生变化,但是当我使用#body:_body时,所选项目不变。

这是我的代码:

class _DropdownmenuScreenState extends State<DropdownmenuScreen> {
  String _currentItemSelected;
  Widget _body;
  Widget _buildContent() {
    return Column(
      children: <Widget>[
        Padding(
            padding: EdgeInsets.symmetric(horizontal: 100, vertical: 100),
            child: FutureBuilder(
              future: fetchspec(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  return DropdownButton<String>(
                    hint: Text("Select Specialite"),
                    value: _currentItemSelected,
                    items: (snapshot.data as List<String>).map((String hm) {
                      return new DropdownMenuItem<String>(
                        value: hm,
                        child: new Text(
                          hm,
                          style: new TextStyle(color: Colors.black),
                        ),
                      );
                    }).toList(),
                    onChanged: changedDropDownStateX,
                  );
                }
                return Center(
                  child: SizedBox(
                    width: 50,
                    height: 50,
                    child: CircularProgressIndicator(
                      semanticsLabel: "Loading ...",
                    ),
                  ),
                );
              },
            )),
      ],
    );
  }

  void changedDropDownStateX(String selectedStateX) {
    setState(() {
      _currentItemSelected = selectedStateX;
    });
  }

  @override
  void initState() {
    super.initState();
    _body = _buildContent();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _body,
    );
  }
}

0 个答案:

没有答案