【flutter】多选

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ivy_doudou/article/details/97925598
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;
  }
  //*************************多选****************************

猜你喜欢

转载自blog.csdn.net/ivy_doudou/article/details/97925598
今日推荐