Flutter: for loop or if-statement is not working perfectly

118

Change the key of questionList["answer"][i]['is_select'] with isSelect

                                   else {
                                        setState(() {
                                          questionList["answer"][i]
                                              ["is_selected"] = false;
                                        });
                                      }
Share:
118
Abir Ahsan
Author by

Abir Ahsan

Updated on December 28, 2022

Comments

  • Abir Ahsan
    Abir Ahsan over 1 year

    I want a create a live exam app. Here is some question in a list and some answer under those question. Users can select only one answer from per question. That why I write a for loop , for check box select. Answer is select perfectly, but else option of if()else condition is not working perfectly. Where is my problem ? Please someone help me.

    Here is my Code-

    import 'package:flutter/material.dart';
    
    class AnswerSheet extends StatefulWidget {
      @override
      _AnswerSheetState createState() => _AnswerSheetState();
    }
    
    class _AnswerSheetState extends State<AnswerSheet> {
      List answerSheet = [
        {
          "qNo": 1,
          "question": "Who are you ?",
          "answer": [
            {"ans": "Teacher", "isSelect": false},
            {"ans": "Student", "isSelect": false},
            {"ans": "Farmer", "isSelect": false},
            {"ans": "Developer", "isSelect": false},
          ]
        },
        {
          "qNo": 2,
          "question": "Where are you from ?",
          "answer": [
            {"ans": "India", "isSelect": false},
            {"ans": "Bangladesh", "isSelect": false},
            {"ans": "India", "isSelect": false},
            {"ans": "China", "isSelect": false},
          ]
        },
        {
          "qNo": 3,
          "question": "Where you born ?",
          "answer": [
            {"ans": "Dhaka", "isSelect": false},
            {"ans": "Barguna", "isSelect": false},
            {"ans": "Patuakhali", "isSelect": false},
            {"ans": "Gazipur", "isSelect": false},
          ]
        },
      ];
    
      @override
      Widget build(BuildContext context) {
        double _width = MediaQuery.of(context).size.width;
        double _height = MediaQuery.of(context).size.height;
        return Scaffold(
          body: Container(
            height: _height,
            width: _width,
            child: SingleChildScrollView(
              child: Column(
                children: answerSheet.map((questionList) {
                  return Padding(
                    padding: const EdgeInsets.all(20.0),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Text(questionList["qNo"].toString()),
                            SizedBox(
                              width: 5,
                            ),
                            Text(
                              questionList["question"],
                              style: TextStyle(fontSize: 28),
                            ),
                          ],
                        ),
                        ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: questionList["answer"].length,
                            itemBuilder: (buildContext, index) {
                              var answerIndex = questionList["answer"][index];
                              return Row(
                                children: [
                                  Checkbox(
                                      value: answerIndex["isSelect"],
                                      onChanged: (value) {
                                        print(
                                            "Length is ${questionList["answer"].length}");
                                        for (var i = 0;
                                            i < questionList["answer"].length;
                                            i++) {
                                          print(
                                              "$i = ${questionList["answer"].indexOf(answerIndex)}");
                                          if (i ==
                                              questionList["answer"].indexOf(
                                                  questionList["answer"][index])) {
                                            setState(() {
                                              answerIndex["isSelect"] = value;
                                            });
                                          } else {
                                            setState(() {
                                              questionList["answer"][i]
                                                  ["is_selected"] = false;
                                            });
                                          }
                                        }
    
                                        print(answerIndex["isSelect"]);
                                      }),
                                  Container(
                                    color: answerIndex["isSelect"] == true
                                        ? Colors.green
                                        : null,
                                    child: Text(
                                      answerIndex["ans"] ?? "",
                                      style: TextStyle(fontSize: 24),
                                    ),
                                  )
                                ],
                              );
                            })
                      ],
                    ),
                  );
                }).toList(),
              ),
            ),
          ),
        );
      }
    }