我正在从 JSON 获取项目并将它们显示在下拉列表中。当这个人从下拉列表中选择一个项目时,我得到了选择,但所选项目没有改变。
例如,我们在列表中有 (tokyo, paris, new york)。默认选择是东京。当这个人选择巴黎时,我明白了,但下拉列表中的选择没有改变。
这是我的代码:
new DropdownButton(
value: cities.elementAt(0),
hint: new Text("Ville"),
items: cities.map((String value) {
return new DropdownMenuItem(
value: value,
child: new Row(
children: <Widget>[
new Icon(
Icons.location_city,
color: Colors.deepOrange,
),
new Text(value)
],
),
);
}).toList(),
onChanged: (String value) {
getTravelCity(value);
},
),
Run Code Online (Sandbox Code Playgroud)
当人选择一个项目时,它仍然显示默认值。
我尝试选择DropdownButton item这样的:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:dropdown_test_sample/main.dart';
void main() {
testWidgets('Select Dropdown item test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const SampleApp());
final dropdown = find.byKey(const ValueKey('dropdown'));
await tester.tap(dropdown);
await tester.pumpAndSettle();
final dropdownItem = find.byKey(const ValueKey('First item key'));
await tester.tap(dropdownItem);
await tester.pumpAndSettle();
});
}Run Code Online (Sandbox Code Playgroud)
似乎有某种东西不断使用相同的DropdownButton item密钥创建相同的密钥,从而使小部件测试失败,因为tester.tap()无法同时“点击”两个小部件。
这是小部件的完整实现DropdownButton:
import 'package:flutter/material.dart';
void main() {
runApp(const SampleApp());
}
class SampleApp extends StatelessWidget {
const SampleApp({Key? key}) …Run Code Online (Sandbox Code Playgroud)