微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 DataTable Flutter 中过滤数据

如何解决在 DataTable Flutter 中过滤数据

enter image description here

我已经实现了对单个列的排序,但我想使用 DataTable 过滤器添加对整个 DropDown 的过滤

这是我的DropDown代码

              Row(
                children: [
                  DropdownButton<String>(
                    value: _chosenSubCounty,//elevation: 5,style: TextStyle(color: Colors.black),items: <String>['Mvita','Tudor','Kisauni'].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,child: Text(value,style: TextStyle(
                            color: Colors.black,fontSize: 16,fontWeight: FontWeight.w600),),);
                    }).toList(),hint: Text(
                      "Sub-County",style: TextStyle(
                          color: Colors.black,onChanged: (String value) {
                      setState(() {
                        _chosenSubCounty = value;
                      });
                    },SizedBox(
                    width: 20,DropdownButton<String>(
                    value: _chosenGender,items: <String>['Male','Female'].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,style: TextStyle(
                              color: Colors.black,hint: Text(
                      "Gender",onChanged: (String value) {
                      setState(() {
                        _chosenGender = value;
                      });
                    },DropdownButton<String>(
                    value: _chosenMembership,items: <String>['Youth Member below 18','Youth Member above 18','Ordinary Member','Life Member'].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,hint: Text(
                      "Membership Type",onChanged: (String value) {
                      setState(() {
                        _chosenMembership = value;
                      });
                    },DropdownButton<String>(
                    value: _chosenDesignation,items: <String>['CHV','RCAT','Staff','Ordinary'].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,hint: Text(
                      "Designation",onChanged: (String value) {
                      setState(() {
                        _chosenDesignation = value;
                      });
                    },],

这是我的DataTable

代码
              Expanded(
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,child: DataTable(
                    sortAscending: isAscending,sortColumnIndex: 1,columnSpacing: 35.0,columns: [
                      DataColumn(
                        label: Text('Name',style: TextStyle(color: Colors.black,/* fontSize: 20.0,*/fontWeight: FontWeight.w700),onSort: (index,b){
                          setState(() {
                            // names.sort((a,b) { a.name.compareto(b.name)});
                          });
                        },DataColumn(
                        label: Text('Age',numeric: true,onSort: (columnIndex,ascending){
                          print(ascending);

                          setState(() {
                            this.isAscending = ascending;
                            ascending ? volunteers.sort((a,b) => a.age.compareto(b.age)) : volunteers.sort((a,b) => b.age.compareto(a.age));
                          });
                        },DataColumn(
                        label: Text('Gender',DataColumn(
                        label: Text('Phone',DataColumn(
                        label: Text('ID',DataColumn(
                        label: Text('Sub-County',DataColumn(
                        label: Text('Membership',DataColumn(
                          label: Text('Designation',rows: volunteers.map((volunteer) =>
                      DaTarow(
                        cells: [
                          DataCell(Text(volunteer.name)),DataCell(Text(volunteer.age.toString())),DataCell(Text(volunteer.gender)),DataCell(Text(volunteer.phone_number)),DataCell(Text(volunteer.id_number)),DataCell(Text(volunteer.sub_county)),DataCell(Text(volunteer.membership_type)),DataCell(Text(volunteer.designation_type)),])).toList()
                  ),

这是我的数据的代码

class Volunteer {


String name;
  int age;
  String gender;
  String phone_number;
  String id_number;
  String sub_county;
  String membership_type;
  String designation_type;

  Volunteer({this.name,this.age,this.gender,this.phone_number,this.id_number,this.sub_county,this.membership_type,this.designation_type});
}

var volunteers = <Volunteer> [
  Volunteer(name: "Abud Said",age: 25,gender: "Male",phone_number: "024061136",id_number: "34303098",sub_county: "Mvita",membership_type: "Life Member",designation_type: "Ordinary"),Volunteer(name: "Abud Said",age: 15,Volunteer(name: "Said Zuher Abud",phone_number: "022061136",age: 35,Volunteer(name: "Zuher Abud Said",Volunteer(name: "Ibtisam Abud Said",age: 45,phone_number: "024067136",];

解决方法

选择过滤器后,您可以修改志愿者列表。

您可能希望保留一份志愿者列表的副本,并将其称为已过滤的志愿者,以便保留原始列表并在未选择过滤器时再次使用该列表。

mov

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。