1

i'm crating an app where the user can make different choices from a list of questions , just the dropdownbutton won't keep the chosen answers after the user scrolls down to the other questions check the GIF enter image description here

i' working with the Provider state management system , here is the code :

import 'package:flutter/material.dart';

class ChoiceHandler extends ChangeNotifier {
  final List<String> _dropdownElements = ['Not Done', 'Partially Done', 'Done'];
  List<String> get dropdownElement => _dropdownElements;
  late String _selectedItemValue;
  String get selected => _selectedItemValue;

  selectedValues(String s) {
    _selectedItemValue = s;
    notifyListeners();
  }
}

and here is the dropdownbutton widget code :

Expanded(
            child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, index) {
                String dropdownValue = "Not Done";

                return ListTile(
                    title: Text(propositions[index]),
                    trailing: Consumer<ChoiceHandler>(
                      builder: (_, provider, __) {
                        return DropdownButton<String>(
                          value: dropdownValue,
                          onChanged: (newValue) {
                            dropdownValue = newValue as String;
                            Provider.of<ChoiceHandler>(context, listen: false)
                                .selectedValues(dropdownValue);

                            print((propositions[index]) + "  " + newValue);
                            dropdown_answer.add(dropdownValue);
                          },
                          items: provider.dropdownElement
                              .map<DropdownMenuItem<String>>((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                        );
                      },
                    ) //_dropdown(index),

                    );
              },
            ),

1 Answers1

2

don't intialize a variable like this String dropdownValue = "Not Done"; in listview builder. just add your status variable in your listing object

class Document {
 String title;
 String data;
 String status;
  Document({
    required this.title,
    required this.data,
    this.status="Not Done",
  });
}

then use

 List<Document> propositions=[];

with

Expanded(
            child: ListView.builder(
              itemCount: propositions.length,
              itemExtent: 50.0,
              itemBuilder: (BuildContext context, index) {

               // now don't need to user this variable
               // String dropdownValue = "Not Done";

                return ListTile(
                    title: Text(propositions[index]),
                    trailing: Consumer<ChoiceHandler>(
                      builder: (_, provider, __) {
                        return DropdownButton<String>(
                          // instead use status property from propositions[index] object
                          value: propositions[index].status,
                          onChanged: (newValue) {
                            propositions[index].status = newValue as String;
                            Provider.of<ChoiceHandler>(context, listen: false)
                                .selectedValues(propositions[index].status);

                            print((propositions[index]) + "  " + newValue);
                            dropdown_answer.add(propositions[index].status);
                          },
                          items: provider.dropdownElement
                              .map<DropdownMenuItem<String>>((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                        );
                      },
                    ) //_dropdown(index),

                    );
              },
            ),

and status value will be stored and maintain status even when widget re-builds.

Jay
  • 206
  • 1
  • 5