ListView.builder中的SwitchListTile和Container或RaisedButton无法正常工作

时间:2020-05-13 13:48:52

标签: flutter

我有一个 Dialog 对话框,我想在其中显示可以分配给员工的不同名称。

在一开始,我尝试仅使用 RaisedButton 选择所需的名称。在应用程序中,按钮应更改颜色。此部分位于 StatefulWidget 中。

我还尝试了一个修改后的版本,在该版本中,我仅为 Dialog 部分创建了一个新的 StatefulWidget ,但该部分没有任何效果,因此我想实现 SwitchListTile 做同样的事情。

SwitchListTile 被激活和停用,尽管仅 注册了真实值。这意味着当我停用(向左滑动)时,代码不会进入以下 setState

setState(() { hEnabled[hDesignations[index].designation] = value; });

此外,当 hEnabled 映射在 setState 方法中更改时,以下代码也不会重新运行以更改容器的颜色:

color: hEnabled[hDesignations[index].designation] ? Colors.green : Colors.grey,

对话框的一部分:

  Widget buildChooseDesignations(
      BuildContext context, List<Designation> hDesignations) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadiusDirectional.circular(8.0),
      ),
      child: _buildDialogChild(context, hDesignations),
    );
  }

  _buildDialogChild(BuildContext context, List<Designation> hDesignations) {
    //todo: when editing an employee I need the chosen designations (have to pass a list)
    Map<String, bool> hEnabled = new Map<String, bool>();
    for (var i = 0; i < hDesignations.length; i++) {
      hEnabled[hDesignations[i].designation] = false;
    }
    return Container(
      height: 200.0,
      //todo: width not working properly
      width: 50,
      child: Column(
        children: <Widget>[
          Expanded(
            child: ListView.builder(
                itemCount: hDesignations.length,
                itemBuilder: (context, index) {
                  return Row(
                    children: <Widget>[
                      Expanded(
                        child: Container(
                          width: 10,
                          color: hEnabled[hDesignations[index].designation] 
                              ? Colors.green
                              : Colors.grey,
                          padding: EdgeInsets.only(left: 80),
                          child: Text(hDesignations[index].designation,
                          style: TextStyle(fontWeight: FontWeight.bold),),
                        ),
                      ),
                      Expanded(
                        child: SwitchListTile(
                            value: hEnabled[hDesignations[index].designation],
                            onChanged: (bool value) {
                              setState(() {
                                hEnabled[hDesignations[index].designation] =
                                    value;
                              });
                            }),
                      )
                    ],
                  );
                }),
          ),
          SizedBox(
            height: 15.0,
          ),
          RaisedButton(
            color: Colors.blueGrey,
            child: Text(
              'set',
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () {
              //todo: save the 'newly' selected designations in a list on set click
            },
          )
        ],
      ),
    );
  }

当我单击 Add + FlatButton时,将调用该对话框,如下所示:

ButtonTheme(
                      height: 30.0,
                      // child: Container(),
                      child: FlatButton(
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        color: Colors.blueGrey.shade200,
                        onPressed: () {
                          //todo add Dialog
                          // List<Designation> hList = state.designations;
                          showDialog(
                              context: context,
                              builder: (context) => buildChooseDesignations(
                                  context, state.designations));
                          // DesignationDialog(
                          //      designations:state.designations));
                        },
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8.0)),
                        child: Text(
                          'Add +',
                          style: TextStyle(color: Colors.black),
                        ),
                      ),
                    ),

enter image description here

1 个答案:

答案 0 :(得分:0)

发现问题了:)

首先,我确实将所有内容重新写入了新的 StatefulWidget 。我需要这样做是因为我希望在单击 SwitchListTile 为容器重新着色后重新构建窗口小部件。

然后,我不得不将我的 hEnabled (重命名为 hChecked )地图移到该州以外。原因是该小部件将重新构建所有内容,包括此地图的初始化,从而使用户的输入无用。

RaisedButton 小部件也是如此。

这是我的代码:

class DesignationDialog extends StatefulWidget {
  final List<Designation> designations;

  final Map<String, bool> hChecked;

  DesignationDialog({Key key, this.designations, this.hChecked}) : super(key: key);

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

class _DesignationDialogState extends State<DesignationDialog> {
  _buildDialogChild(BuildContext context, List<Designation> hDesignations) {
    //todo: when editing an employee I need the chosen designations (have to pass a list)
    // for (var i = 0; i < hDesignations.length; i++) {
    //   hChecked[hDesignations[i].designation] = false;
    // }
    return Container(
      height: 200.0,
      child: Column(
        children: <Widget>[
          Expanded(
            child: ListView.builder(
                itemCount: hDesignations.length,
                itemBuilder: (context, index) {
                  // return ButtonTheme(
                  //   //todo: fix the width of the buttons is not working
                  //   minWidth: 20,
                  //   child: RaisedButton(
                  //     color: widget.hChecked[hDesignations[index].designation]
                  //         ? Colors.green
                  //         : Colors.grey,
                  //     child: Text(hDesignations[index].designation),
                  //     onPressed: () {
                  //       //todo mark designation and add to an array
                  //       setState(() {
                  //         widget.hChecked[hDesignations[index].designation] =
                  //             !widget
                  //                 .hChecked[hDesignations[index].designation];
                  //       });
                  //     },
                  //   ),
                  // );

                  // -- With Switch
                  return Row(
                    children: <Widget>[
                      Expanded(
                        child: Container(
                          child: Text(hDesignations[index].designation),
                          width: 10,
                          color: widget.hChecked[hDesignations[index].designation]
                              ? Colors.green
                              : Colors.grey,
                        )),
                         Expanded(
                        child: SwitchListTile(
                            value: widget.hChecked[hDesignations[index].designation],
                            onChanged: (bool value) {
                              setState(() {
                                widget.hChecked[hDesignations[index].designation] =
                                    value;
                              });
                            }),
                      )
                    ],
                  );
                  // -- end
                }),
          ),
          SizedBox(
            height: 15.0,
          ),
          RaisedButton(
            color: Colors.blueGrey,
            child: Text(
              'set',
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () {
              //todo: save the 'newly' selected designations in a list on set click
            },
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadiusDirectional.circular(8.0),
      ),
      child: _buildDialogChild(context, widget.designations),
    );
  }