dimanche 11 juillet 2021

How can i use select all Checkbox in flutter?

I have created Two classes with State Management, one class to display output after tick the checkbox and the other class for tick the checkbox. How can I put for select all checkbox? I have tried this http://5.9.10.113/65445597/how-to-make-a-button-select-all-flutter-checkboxes but there is some error because I have separated input and output classes.

Class for output:

class AbFour extends StatefulWidget {
  @override
  _AbFourState createState() => _AbFourState();
}

class _AbFourState extends State<AbFour> {
  List<Exercise> exercises = [
    Exercise(
        "« 3 sets 10 repetition »",
        "Dumbbell Swing",
        'DumbbellSwing',
        'assets/images/dumbbell swing.gif',
        "Hold a dumbbell in both hands. Bend from the hips to lower the weight between your legs, then push your hips forward to raise it up to shoulder height. Reverse back down to the start.",
        false),
    Exercise(
        "« 3 sets 10 repetition »",
        "Dumbbell Side Bend",
        'SideBend',
        'assets/images/side bend.gif',
        "Stand tall, holding the dumbbell in one hand. Keeping your chest up, lower the weight and it will hit your obliques. Complete all the reps, then switch hands and repeat.",
        false),
    Exercise(
        "« 3 sets 10 repetition »",
        "Dumbbell High Ab Crunch",
        'HighAbCrunch',
        'assets/images/high ab crunch.gif',
        "Lie on the floor or a mat on your back, with knees bent. Grasp a lightweight dumbbell in two hands, palms facing out, arms extended straight into the air above your shoulders. Feet should be flat on the ground. Keep a space between your chin and chest.",
        false),
    Exercise(
        "« 3 sets 10 repetition »",
        "Dumbbell Crunch",
        'DumbbellCrunch',
        'assets/images/dumbbell crunch.gif',
        "Lie flat on the floor with your knees bent, holding the dumbbell to your chest with both hands. Use your upper abs to raise your torso, then lower slowly to the start.",
        false),
    Exercise(
        "« 3 sets 10 repetition »",
        "Dumbbell Russian Twist",
        'DumbbellRussianTwist',
        'assets/images/dumbbell russian twist.gif',
        "Start at the top of the crunch but with your feet off the ground. Rotate back and forth, keeping your abs braced by holding the kettlebell. A twist to one side then the other counts as one rep.",
        false),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("LIST EXERCISE"),
      ),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: getProportionateScreenHeight(20),
              ),
              Row(
                children: const <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: (20)),
                    child: Text("Name",
                        style: TextStyle(
                          fontSize: 18.0,
                          color: kPrimaryLightColor,
                        )),
                  ),
                  Spacer(),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: (20)),
                    child: Text("5/5",
                        style: TextStyle(
                          fontSize: 18.0,
                        )),
                  ),
                  Spacer(),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: (20)),
                    child: Text("Preview",
                        style: TextStyle(
                          fontSize: 18.0,
                          color: kPrimaryLightColor,
                        )),
                  ),
                ],
              ),
              SizedBox(
                height: getProportionateScreenHeight(10),
              ),
              ...exercises
                  .map(
                    (exercise) => ListExercise(
                      exercise: exercise,
                      onChanged: (value) {
                        setState(() {
                          exercise.selected = value;
                        });
                      },
                    ),
                  )
                  .toList(),
              

class for input:

class ExerciseAbFour extends StatefulWidget {
  final List<Exercise> exercises;
  ExerciseAbFour(this.exercises);

  @override
  _ExerciseAbFourState createState() => _ExerciseAbFourState();
}

class _ExerciseAbFourState extends State<ExerciseAbFour> {
  int currentPage = 0;
  
  String done = "Abs exercise using dumbbell";
  @override
  Widget build(BuildContext context) {
    var selectedExercises =
        widget.exercises.where((element) => element.selected);

    return Scaffold(
        appBar: AppBar(
            title: Text(
          "DAILY FITNESS",
        )),
        body: Padding(
          padding:
              EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(10)),
          child: Container(
              child: Column(children: [
            Expanded(
              flex: 5,
              child: PageView.builder(
                onPageChanged: (value) {
                  setState(() {
                    currentPage = value;
                  });
                },
                itemCount: selectedExercises.length,
                itemBuilder: (context, index) => PageView(
                  children: <Widget>[
                    Column(
                      children: [
                        SizedBox(
                          height: getProportionateScreenHeight(10),
                        ),
                        Text(
                          "Ready to go!",
                          style: TextStyle(
                            fontSize: 25,
                            color: kPrimaryColor,
                          ),
                        ),
                        Text(
                          "Exercise:",
                          style: TextStyle(
                            fontSize: 20,
                          ),
                        ),
                        Text(
                          selectedExercises.toList()[index].name,
                          style: TextStyle(
                            fontSize: 20,
                          ),
                        ),
                        SizedBox(
                          height: getProportionateScreenHeight(10),
                        ),
                        Container(
                            child: ClipRRect(
                          borderRadius: BorderRadius.circular(10.0),
                          child: Image.asset(
                            selectedExercises.toList()[index].image,
                            width: getProportionateScreenWidth(365),
                          ),
                        )),
                        SizedBox(
                          height: getProportionateScreenHeight(15),
                        ),
                        Text(
                          selectedExercises.toList()[index].rep,
                          style: TextStyle(
                            fontSize: 20,
                          ),
                        ),
                        SizedBox(
                          height: getProportionateScreenHeight(15),
                        ),
                        Text(
                          selectedExercises.toList()[index].description,
                          textAlign: TextAlign.justify,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 1,
              child: Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: getProportionateScreenWidth(10)),
                child: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: List.generate(selectedExercises.toList().length,
                          (index) => buildDot(index: index)),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              height: getProportionateScreenHeight(10),
            ),
            DefaultButton(
              text: "Done",
              press: () {
                inputData();
                showDialog(
                  context: context,
                  barrierDismissible: false, // user must tap button!
                  builder: (BuildContext context) {
                    return AlertDialog(
                        shape: RoundedRectangleBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(16.0))),
                        contentPadding: EdgeInsets.all(0.0),
                        content: Container(
                          width: getProportionateScreenWidth(200),
                          height: getProportionateScreenHeight(240),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              SizedBox(
                                  height: getProportionateScreenHeight(0.1)),
                              Text(
                                'Congratulation!',
                                style: TextStyle(
                                    fontSize: 26.0,
                                    letterSpacing: 1.0,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.greenAccent[400]),
                                textAlign: TextAlign.center,
                              ),
                              Divider(
                                color: Colors.grey,
                              ),
                              Container(
                                padding: EdgeInsets.symmetric(
                                    vertical: 1.0, horizontal: 0.0),
                                child: Text(
                                  "Nice you have done it!",
                                  style: TextStyle(
                                    fontSize: 20.0,
                                  ),
                                  textAlign: TextAlign.center,
                                ),
                              ),
                              InkWell(
                                onTap: () {
                                  Navigator.pushAndRemoveUntil(
                                      context,
                                      MaterialPageRoute(
                                        builder: (context) => HomeScreen(),
                                      ),
                                      (route) => false);
                                },
                                child: Container(
                                  padding:
                                      EdgeInsets.only(top: 20.0, bottom: 20.0),
                                  decoration: BoxDecoration(
                                    color: kPrimaryColor,
                                    borderRadius: BorderRadius.only(
                                        bottomLeft: Radius.circular(16.0),
                                        bottomRight: Radius.circular(16.0)),
                                  ),
                                  child: Text(
                                    "Next",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 18.0,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              )
                            ],
                          ),
                        ));
                  },
                );
              },
            ),
            SizedBox(
              height: getProportionateScreenHeight(20),
            ),
          ])),
        ));
  }

Class for declared list:

class ListExercise extends StatelessWidget {
  final Exercise exercise;
  final Function onChanged;

  ListExercise({
    Key key,
    this.exercise,
    this.onChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var route = exercise.route;
    return Container(
      child: Column(
        children: [
          const Divider(
            height: 20,
            thickness: 1,
            indent: 15,
            endIndent: 15,
          ),
          Row(
            children: [
              Expanded(
                child: CheckboxListTile(
                  title: Text(exercise.name),
                  secondary: IconButton(
                    icon: SvgPicture.asset("assets/icons/review.svg"),
                    onPressed: () {
                      Navigator.pushNamed(context, '/$route');
                    },
                  ),
                  selectedTileColor: kPrimaryColor,
                  value: exercise.selected,
                  onChanged: onChanged,
                  controlAffinity: ListTileControlAffinity.leading,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}




Aucun commentaire:

Enregistrer un commentaire