【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;
  }
  //*************************多选****************************
Flutter中的`ListView`并不支持多选,但我们可以通过自定义`ListView`来实现多选功能。 首先,我们需要定义一个数据模型类,来表示每一个列表项。这个类至少需要包含两个属性:`title`和`selected`,分别表示列表项的标题和是否被选中。 ```dart class ListItem { String title; bool selected; ListItem({this.title, this.selected = false}); } ``` 然后,我们可以使用`ListView.builder`来构建列表。在构建列表项时,我们需要根据`selected`属性来决定是否显示选中状态的图标。 ```dart ListView.builder( itemCount: items.length, itemBuilder: (BuildContext context, int index) { return InkWell( onTap: () { setState(() { items[index].selected = !items[index].selected; }); }, child: Row( children: [ Checkbox( value: items[index].selected, onChanged: (_) { setState(() { items[index].selected = !items[index].selected; }); }, ), Text(items[index].title), if (items[index].selected) Icon(Icons.check), ], ), ); }, ) ``` 在以上代码中,使用了`InkWell`组件来监听列表项的点击事件,并在点击时修改`selected`属性。同时,使用了`Checkbox`组件来实现选中状态的切换,当然你也可以使用其他组件来替代它。 最后,我们需要在`State`类中维护一个`items`列表来存储所有的列表项。在多选操作完成后,我们可以通过遍历`items`列表来获取所有被选中的列表项。 ```dart List<ListItem> items = [ ListItem(title: 'Item 1'), ListItem(title: 'Item 2'), ListItem(title: 'Item 3'), ]; List<ListItem> getSelectedItems() { return items.where((item) => item.selected).toList(); } ``` 这样,我们就完成了一个基本的多选列表。当然,你还可以为列表项添加更多的属性和功能,让它更加丰富和实用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值