【发布时间】:2020-08-29 18:01:03
【问题描述】:
我有一个 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),
),
),
),
【问题讨论】:
标签: flutter