我试图创建小部件闪屏,显示何时应用程序加载和本机闪屏显示.我创建了闪屏,一切似乎都很好,所以我决定为闪屏背景图像和屏幕上的单个文本创建小部件测试.文本的测试部分通过了,但我对图像有问题.
Widget test error text:个
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "Image" (ignoring offstage widgets)>
Which: means none were found but one was expected
The full splash screen code:个
class SplashScreenPage extends StatefulWidget {
const SplashScreenPage({Key? key}) : super(key: key);
@override
State<SplashScreenPage> createState() => _SplashScreenPageState();
}
class _SplashScreenPageState extends State<SplashScreenPage> {
Timer? _timer;
@override
void initState() {
_timer = Timer(const Duration(seconds: 4), () {
Navigator.of(context).pushReplacementNamed(RoutingConst.defaultRoute);
});
super.initState();
}
@override
void dispose() {
_timer!.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background.png"),
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
const Spacer(),
const Spacer(),
const Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: Text(
"SPLASH SCREEN TEXT",
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: GoogleFonts.inter(
color: Colors.white,
fontSize: 40,
fontWeight: FontWeight.bold,
),
),
),
],
),
const Spacer(),
],
),
),
]
),
);
}
}
The widget test:个
void main() {
//
// Setup
//
Widget createWidgetUnderTest() {
return const MaterialApp(
home: SplashScreenPage(),
);
}
//
// Testing
//
testWidgets(
"Check if splash screen text shows up",
(WidgetTester tester) async {
await tester.pumpWidget(createWidgetUnderTest());
expect(find.text("SPLASH SCREEN TEXT"), findsOneWidget);
},
);
testWidgets(
"Check if splash screen background shows up",
(WidgetTester tester) async {
await tester.pumpWidget(createWidgetUnderTest());
expect(find.byType(Image), findsOneWidget);
},
);
}
文本的测试通过了,但图像的测试失败了,我不知道为什么.我try 了find.Image(AssetImage("ASSET-PATH")),但效果不佳.试着把 keys 放在装有盒子装饰的容器上,然后在测试中试图找到容器,它发现它可能是我在寻找盒子装饰内部的图像时做了一些错误的事情,但我不知道如何找到它,如果是这样的话.