flutter dropdownbutton在滚动后不会保留答案

r3i60tvu  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(103)

我正在创建一个应用程序,用户可以从一个问题列表中做出不同的选择,只是下拉按钮不会保留所选的答案后,用户向下滚动到其他问题检查GIF

i'使用提供程序状态管理系统,代码如下:

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();
  }
}

下面是下拉按钮控件代码:

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),

                    );
              },
            ),
wbgh16ku

wbgh16ku1#

不要初始化这样的变量String dropdownValue =“Not Done”;在列表视图生成器中.只需在列表对象中添加状态变量

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

则使用

List<Document> propositions=[];

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),

                    );
              },
            ),

并且状态值将被存储,并且即使在部件重建时也保持状态。

相关问题