I am just getting started with flutter widget testing and came upon this issue. I was wondering if this is expected behavior. When trying to find my TextField with find.byType it succeeds but with find.byWidget it doesn't. Is this normal or am I doing something wrong? The goal is later to enter text in the textfield and tap a button after.
My textField and button:
Column(
children: [
TextField(
style: TextStyle(
color: Colors.black,
),
autofocus: true,
autocorrect: false,
enableSuggestions: false,
controller: _controller,
cursorColor: Colors.black,
decoration: InputDecoration(
hintText: 'Fill in.....',
hintStyle: TextStyle(fontSize: 18),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton(
onPressed: () {
onSubmit(_controller.text);
},
child: Text('Press me!',
style: TextStyle(
fontSize: 18,
color: Theme.of(context).primaryColorBrightness ==
Brightness.dark
? Colors.white
: Colors.black,
))),
],),
],
),
And this is my test:
tester.ensureVisible(find.byType(TextInputWidget));
expect(find.byType(TextInputWidget), findsOneWidget);
final a = TextField(
style: TextStyle(
color: Colors.black,
),
autofocus: true,
autocorrect: false,
enableSuggestions: false,
controller: TextEditingController(),
cursorColor: Colors.black,
decoration: InputDecoration(
hintText: 'Fill in.....',
hintStyle: TextStyle(fontSize: 18),
),
);
//expect(find.byWidget(a), findsOneWidget); // Fails
expect(find.byType(TextField), findsOneWidget); //Succeeds
Parts of your test are missing, but it looks like you are trying to find widget a
in your tester where it is not known.
If you want to find a widget by its variable reference, you have to use the same reference when you define the widget that is then passed to pumpWidget()
method like so:
final a = TextField(...);
tester.pumpWidget(Container(
child: a,
));
expect(find.byWidget(a), findsOneWidget);