我正在为我的一个 flutter-web 应用程序开发一些集成测试。考虑到从上到下的方法,我想在下面的小部件层次结构树中专门定位容器(宽度:50,高度:50)。ParentWidget --> Row --> Text, SizedBox, Row --> Container(), Container(width: 50, height: 50) , Container()。 注:无键方法
我尝试使用 ParentWidget --> Row --> Row --> with index 的后代进行定位,尝试选择第二个容器,但这不起作用,它失败并出现错误"Bad state: No element"。
import 'package:flutter/material.dart';
class ParentWidget extends StatelessWidget {
const ParentWidget({Key? key});
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("Hello World..!!"),
SizedBox(
width: 20,
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
width: 20,
height: 20,
color: Colors.red,
),
Container(
width: 50,
height: 50,
color: Colors.red,
),
Container(
width: 20,
height: 20,
color: Colors.red,
),
],
),
],
);
}
}
final container = tester.widget<Container>(find.descendant(
of: find.descendant(
of: find.byType(ParentWidget), matching: find.byType(Row)),
matching: find.descendant(
of: find.byType(Row), matching: find.byType(Container).at(1))));