my problem is that I have a stateless widget that returns a AppBar
with an onPressed event. In my corresponding widgetTest, I tap the component and expect that the onPressed method is called. However, it is not.
This is my widget:
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
class IconAppBar extends StatelessWidget with PreferredSizeWidget {
final VoidCallback onPressed;
IconAppBar({this.onPressed});
@override
Widget build(BuildContext context) {
return AppBar(
leading: IconButton(
color: Colors.black,
icon: SvgPicture.asset(
"resources/images/icon.svg",
width: 24,
height: 24,
),
onPressed: onPressed,
),
);
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
In the test, I create the widget with a simple test for the onPressed if the component was tapped.
void main() {
testWidgets("Should run onPressed when tapped",
(WidgetTester tester) async {
var counter = 0;
void callback() {
counter++;
}
await tester.pumpWidget(
MediaQuery(
data: MediaQueryData(),
child: MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: IconAppBar(
onPressed: callback,
),
),
),
);
await tester.pump(Duration(milliseconds: 300));
await tester.tap(find.byType(IconAppBar));
await tester.pump(Duration(milliseconds: 300));
expect(counter, 1); // fails because actual counter is 0
});
}
Changing to tester.pump()
or tester.pumpAndSettle()
did not change anything. The method is not called.
Edit: Running this on an actual device or in an emulator, the onPressed works as expected.
I would really appreciate any help or ideas. Thanks!
Aucun commentaire:
Enregistrer un commentaire