我想知道如何断言特定TextField是否具有焦点。
以下代码失败,因为它说focusNode
是null
。 (我无法编辑Widget代码本身)
final fooKey = Key('foo');
final barKey = Key('bar');
final namePage = MediaQuery(
data: MediaQueryData(),
child: MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
TextField(
key: fooKey,
autofocus: true,
),
TextField(
key: barKey,
),
],
),
),
),
);
testWidgets('Should focus on fooTextField not barTextField', (WidgetTester tester) async {
await tester.pumpWidget(namePage);
expect(tester.testTextInput.isVisible, true);
final fooField = find.byKey(fooKey).evaluate().single.widget as TextField;
final barField = find.byKey(barKey).evaluate().single.widget as TextField;
await tester.enterText(find.byKey(fooKey), 'I am a foo');
expect(fooField.focusNode.hasFocus, true);
expect(barField.focusNode.hasFocus, false);
})