我有一段代码创建了一个Table of Text小部件,如下所示:
return Table(
defaultColumnWidth: FixedColumnWidth(120.0),
children: <TableRow>[
TableRow(
children: <Widget>[Text('toffee'), Text('potato')],
),
TableRow(
children: <Widget>[Text('cheese'), Text('pie')],
),
],
);
我想测试一下表中的第一项确实是“太妃糖”这个词。我设置了测试并进入这一部分:
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.toList()[0].widget;
expect(firstCell, 'toffee');
这肯定不起作用,因为firstCell
是Widget类型,它不等于String toffee
。
我只看到一个toString()
函数,像这样:
'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
textAlign: left)'
如何提取text
属性以获得toffee
这个词?
现在看起来我能做的就是检查那些不理想的.toString().contains('toffee')
。
你可以将你的firstCell
施放到Text
。
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.whereType<Text>()
.first;
然后测试firstCell.data
expect(firstCell.data, 'toffee');
按文字查找?
expect(find.text('toffee'), findsOneWidget);