List<bool> boolList = List<bool>();//暂存多选false/true
List<Map> _selctlist = [
{'name': '农药', 'value': '农药'},
{'name': '化肥', 'value': '化肥'},
{'name': '种子', 'value': '种子'}
];
_getTextFormMulti(TextEditingController controller, String text) {
return GestureDetector(
onTap: () {
_showDialog();
},
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 17, 0),
height: MediaQuery.of(context).size.height / 15,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 3 / 5,
child: TextFormField(
maxLines: 1,
enabled: false,
controller: controller,
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(15, 0, 0, 0),
//内间距
focusedBorder:
OutlineInputBorder(borderSide: BorderSide.none //不显示边框
),
border: OutlineInputBorder(borderSide: BorderSide.none),
hintText: text,
hintStyle: TextStyle(
fontSize: 18.0,
color: Color.fromARGB(255, 125, 125, 125))),
),
),
Container(
child: Icon(
Icons.keyboard_arrow_down,
size: 30.0,
color: font_b_public,
))
],
)),
);
}
//*************************多选****************************
//显示多选框
_showDialog() async {
boolList = List<bool>();
await showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context,state){
return CupertinoAlertDialog(
title: Text('请选择'),
actions: <Widget>[
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () {
Navigator.of(context).pop('取消');
},
child: new Text('取消')),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () {
//保存数据
_jyfw.text = "";
for(int i=0;i<_selctlist.length;i++){
if(boolList[i]){
if(_jyfw.text==null||_jyfw.text.isEmpty){
_jyfw.text = _selctlist[i]['value'];
}else{
_jyfw.text = _jyfw.text + "," +_selctlist[i]['value'];
}
}
}
setState(() {});
Navigator.of(context).pop('确定');
},
child: new Text('确定')),
],
content: SingleChildScrollView(
child: Material(
child: Column(
children: _getMulitiWidget(_selctlist,state)
),
),
),
);
}
);
}
);
}
//多选的组件
List<Widget> _getMulitiWidget(List<Map> list,state){
List<Widget> widgetList = List<Widget>();
for(int i=0;i<list.length;i++){
boolList.add(_jyfw.text.split(",").contains(list[i]['name'])?true:false);
widgetList.add(Container(
child: CheckboxListTile(
title: Text(list[i]['name'],overflow: TextOverflow.ellipsis),
value: boolList[i],
onChanged: (value){
boolList[i] = value;
state(() {});
}
),
));
};
return widgetList;
}
//*************************多选****************************
【flutter】多选
最新推荐文章于 2024-08-20 08:44:10 发布