问题描述
我的问题是我有一个无状态窗口小部件,该窗口小部件返回带有onPressed事件的AppBar
。在相应的widgetTest中,我点击了组件,并期望调用onPressed方法。但是,不是。
这是我的小部件:
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);
}
在测试中,如果轻按了组件,我将为onPressed创建一个简单测试的小部件。
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
});
}
更改为tester.pump()
或tester.pumpAndSettle()
并没有任何改变。
该方法未调用。
在实际设备或仿真器上运行它,onPressed会按预期工作。
另外,使用find.byType(IconAppBar)
进行的冒烟测试确实找到了小部件。
我将非常感谢您的帮助或想法。谢谢!
编辑:看来这与IconButton
不兼容。我只是将其更改为FlatButton
,并且将SVG作为其子元素。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)