Flutter Dropdown Button赢得了滚动后的答案
我正在将用户从一个问题列表中做出不同选择的应用程序,只是在用户向下滚动到其他问题之后,下拉列表不会保留所选答案
我使用提供商状态管理系统,这是代码:
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请勿在此字符串下拉值=“不完成”之类的变量;
在ListView Builder中。
只需在列表对象中添加状态变量
使用并维护状态。
一起
,即使小部件重建时,也将与状态值
don't intialize a variable like this String dropdownValue = "Not Done";
in listview builder.
just add your status variable in your listing object
then use
with
and status value will be stored and maintain status even when widget re-builds.