Flutter Dropdown Button赢得了滚动后的答案

发布于 2025-02-03 06:14:41 字数 2236 浏览 4 评论 0原文

我正在将用户从一个问题列表中做出不同选择的应用程序,只是在用户向下滚动到其他问题之后,下拉列表不会保留所选答案

我使用提供商状态管理系统,这是代码:

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

这是下拉列表button widget代码:

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

                    );
              },
            ),

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

                    );
              },
            ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2025-02-10 06:14:41

请勿在此字符串下拉值=“不完成”之类的变量;
在ListView Builder中。
只需在列表对象中添加状态变量

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

                    );
              },
            ),

,即使小部件重建时,也将与状态值

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文