【flutter】多选

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;
  }
  //*************************多选****************************
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值