数据分配给Flutter中的复选框时,如何选择所有Checkbox?

时间:2019-11-21 06:16:27

标签: flutter

在单击第一个选择器时如何选择所有复选框?

[考虑为复选框]

  

此图片的代码

主页界面

import 'package:flutter/material.dart';
import 'package:teacher_version_1/Navigations/SideDrawer.dart';
import 'package:teacher_version_1/popularFilterList.dart';
import 'AttendanceHistoryDemo.dart';


class StudentAttendanceDemo extends StatefulWidget {
  @override
  _StudentAttendanceDemoState createState() => _StudentAttendanceDemoState();
}

class _StudentAttendanceDemoState extends State<StudentAttendanceDemo> {
  List<PopularFilterListData> accomodationListData = PopularFilterListData.accomodationList;
  GlobalKey _bottomNavigationKey = GlobalKey();
  int _page = 0;
  TextEditingController _searchview = new TextEditingController();
  final fromkey = new GlobalKey<FormState>();
  bool _switchValue = false;
  RangeValues _values = RangeValues(100, 600);
  double distValue = 50.0;

  @override
  Widget build(BuildContext context) {

    double h=MediaQuery.of(context).size.height;
    double w=MediaQuery.of(context).size.width;

    bool valMonday = false;



   return LayoutBuilder(
  builder: (context,c)
      {
        if(c.maxWidth<=600)
          {
            return SafeArea(

              child: Scaffold(
                resizeToAvoidBottomPadding: false,
                body: Column(
                  children: <Widget>[
                    Column(
                      children: <Widget>[
                        Container(
                            decoration: BoxDecoration(
                                color: Color(0xFF343193)
                            ),
                            height: h/18,
                            width: w/1,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(4.0),
                                  child: Container(
                                    // color: Colors.green,
                                      width: w/1.1,
                                      child: _createSearchView()),
                                ),
                                ],
                            )

                        ),
                        Container(
                            decoration: BoxDecoration(

                                color: Color(0xFFEBEBEB)
                            ),
                            height: h/20,
                            width: w/1,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.only(left:25.0),
                                  child: Text("Roll No"),
                                ),
                                Text("Name"),
                                Padding(
                                  padding: const EdgeInsets.only(right:35.0),
                                  child: CupertinoSwitch(
                                    activeColor: Colors.green,
                                    value: _switchValue,
                                    onChanged: (bool value) {
                                      setState(() {
                                        _switchValue = value;
                                      });
                                    },
                                  ),
                                ),

                              ],
                            )

                        ),
                        Container(
                          height: h/1.66,
                          //  color: Colors.orange,
                          child: ListView(

                            children: <Widget>[
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[

                                  Padding(
                                    padding: const EdgeInsets.only(right: 16, left: 16),
                                    child: Column(
                                      children: getAccomodationListUI(),
                                    ),), ], )],), ), ],  ],  ), ),                                     
                               ); }}
                             ); }
  

调用方法

  1. 列表项
  

调用方法

  • 列表项

    列表getAccomodationListUI(){     double h = MediaQuery.of(context).size.height;     TextStyle textStyle = new TextStyle(fontSize:h / 49,fontFamily:'Nunito');     列出noList = List();     for(var i = 0; i

              Material(
                color: Color(0xFFF1F1F1),
                child: InkWell(
                  borderRadius: BorderRadius.all(Radius.circular(4.0)),
                  onTap: () {
                    setState(() {
                      checkAppPosition(i);
                      print("abcd");
                    });
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: Text(
                            date.rollNo,
                            style: textStyle,
                          ),
                        ),
                        Expanded(
                          flex: 2,
                          child: Text(
                            date.titleTxt,
                            style: textStyle,
                          ),
                        ),
                        CupertinoSwitch(
                          activeColor: date.isSelected ? Colors.green : Colors.grey.withOpacity(0.6),
                          onChanged: (value) {
                            setState(() {
                              checkAppPosition(i);
                            });
                          },
                          value: date.isSelected,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
      /* if (i == 0) {
        noList.add(Divider(
          height: 1,
        ));
      }*/
    }
    return noList;
    

    }

    void checkAppPosition(int index){     如果(索引== 0){       如果(accomodationListData [0] .isSelected){         accomodationListData.forEach((d){           d.isSelected = false;         });       }其他{         accomodationListData.forEach((d){           d.isSelected = true;         });       }     }其他{       accomodationListData [index] .isSelected =!accomodationListData [index] .isSelected;

      var count = 0;
      for (var i = 0; i < accomodationListData.length; i++) {
        if (i != 0) {
          var data = accomodationListData[i];
          if (data.isSelected) {
            count += 1;
          }
        }
      }
    
      if (count == accomodationListData.length - 1) {
        accomodationListData[0].isSelected = true;
      } else {
        accomodationListData[0].isSelected = false;
      }
    }
    

    }

    小部件_createSearchView(){

    double h=MediaQuery.of(context).size.height;
    double w=MediaQuery.of(context).size.width;
    
    return Padding(
      padding:  EdgeInsets.only(
        left:w/30.0,
      ),
    
      child: new Container(
        height:h/2,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(30),
            // border: Border.all(width: 1.0),
            color: Colors.white),
        child: new TextFormField(
    
          controller: _searchview,
          decoration: InputDecoration(
    
            prefixIcon: Icon(Icons.search,color: Colors.black,),
            suffixIcon: IconButton(
                icon: Icon(Icons.close,color: Colors.black,),
                onPressed: () {
                  _searchview.clear();
                }),
            contentPadding: new EdgeInsets.symmetric(vertical: 8.0, horizontal: 5.0),
            hintText: "Search students",
            hintStyle: new TextStyle(color: Colors.black,fontSize: MediaQuery.of(context).size.height/50,fontFamily: 'Nunito'),
            border: InputBorder.none,
    
          ),
          //   textAlign: TextAlign.center,
        ),
      ),
    );
    

    }   }

1 个答案:

答案 0 :(得分:0)

只需对所有Toggle使用boolean dataType,而onPress方法只需检查像这样...

if(allToggle){
 setState(() {
toogle1 = true,
toggle2 = true,
toggle3 = true, 
and for rest of all(you can use for loop as well)//
})
} 

希望这对您有用...:-)