0

I am using a variable column whose children has stateful methods, for example:

String word = 'hi';
Column c = Column(
children: [
FlatButton(
child(Text(word)),
onPressed() {
setState({
word += word;
})
}
) // Flat Button
]
);

but setState won't work for me, this is the full code:

 Widget build(BuildContext context) {

    Column questionsForm;
    Map answers = Map();
    Map sortedQuestions;

    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
              padding: const EdgeInsets.all(25.0),
              child: Column(
                children: [
                StreamBuilder(
                  stream: partEnrollment.snapshots(),
                  builder: (context, snapshot) {
                    answers = Map();
                    questionsForm = Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 25,
                        )
                      ],
                    );
                    

                    for (String q in questions.keys) answers[q] = -1;

                    for (String q in questions.keys) {
                 
                        questionsForm.children.add(Text(
                          questions[q],
                          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                        ));
                        questionsForm.children.add(Row(
                          children: [
                            FlatButton(
                              onPressed: () {

                                setState(() {
                                  answers[q] = 0;
                                  print(answers[q]);
                                });

                              },
                              child: Text(
                                'Yes',
                                style: TextStyle(
                                    fontSize: 17,
                                    fontWeight:
                                    answers[q] == 0 ? FontWeight.bold : FontWeight.normal),
                              ),
                            ),
                            FlatButton(
                              onPressed: () {
                                setState(() {
                                  answers[q] = 1;
                                  print(answers[q]);
                                });
                              },
                              child: Text(
                                'No',
                                style: TextStyle(
                                    fontSize: 17,
                                    fontWeight:
                                    answers[q] == 1 ? FontWeight.bold : FontWeight.normal),
                              ),
                            )
                          ],
                        ));
                     
                      }
                    }

                    return questionsForm;
                  },
                ), //Stream builder

I have tried many other ways like putting it in a radio list with its own state builder but it didn't work, and when I press the buttons the number changes, here is a snapshot of the screen: questions form

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Fahad
  • 1
  • 1

1 Answers1

1

I think you maintain the below variables inside the build method. So, It will initialize as a new element every time you (Rebuild) call setState((){}). It will work if you maintain the below variables outside of the build method.

    Column questionsForm;
    Map answers = Map();
    Map sortedQuestions; 
Ashok Kuvaraja
  • 666
  • 3
  • 11