我正在使用:https://pub.dartlang.org/packages/flutter_typeahead。 这是我的代码:
@override
Widget build(BuildContext context) {
return BlocBuilder(bloc: _bloc, builder: (_, state) {
if (state is TypeHeadSearchStateInitialized) {
suggestions = [];
state.value.forEach((element) => suggestions.add(element.name));
}
return TypeAheadFormField(
textFieldConfiguration: TextFieldConfiguration(
controller: _typeAheadController,
decoration: InputDecoration(
prefixIcon: _icon,
hintText: _hintText),
),
suggestionsCallback: (String pattern) async {
return suggestions;
},
itemBuilder: (context, suggestion) {
if (suggestions.isNotEmpty)
return ListTile(
title: Text(suggestion),
);
},
onSuggestionSelected: (suggestion) {
this._typeAheadController.text = suggestion;
},
hideOnEmpty: true,
);
});
}
这是我正在做的测试,目的是检查用户在textField中插入内容后是否显示建议:
testWidgets('Should return a ListTile for suggestions', (WidgetTester tester) async {
await _prepare(tester);
when(_mockService.getAllSuggestions("Name")).thenAnswer((_) async => generateTestResults(1));
await tester.enterText(find.byWidgetPredicate((Widget widget) => widget is TextField), "Name");
await tester.pump();
expect(find.byWidgetPredicate((Widget widget) => widget is ListTile), findsOneWidget);
});
该bloc正常工作,我对其进行了调试,然后将其输入TypeHeadSearchStateInitialized
,并使用
state.value.forEach((element) => suggestions.add(element.name));
但是,构建仅返回TypeAheadFormField,而不构建建议,并且测试失败。