使用package:flutter_test
,我可以创建一个查找器,通过键查找小部件:
expect(find.byKey(const ValueKey('counter')), findsOneWidget);
Run Code Online (Sandbox Code Playgroud)
或通过文字:
expect(find.text('0'), findsOneWidget);
Run Code Online (Sandbox Code Playgroud)
我还可以找到从此小部件衍生的小部件:
expect(
find.descendant(
of: find.byKey(const ValueKey('counter')),
matching: find.text('0'),
),
findsNothing,
);
Run Code Online (Sandbox Code Playgroud)
或者祖先:
expect(
find.ancestor(
of: find.text('0'),
matching: find.byKey(const ValueKey('counter')),
),
findsNothing,
);
Run Code Online (Sandbox Code Playgroud)
但是我如何组合这些查找器来验证是否存在带有“计数器”键且文本为“0”的小部件?例如:
Text(
'$_counter',
key: const Key('counter'),
style: Theme.of(context).textTheme.headline4,
),
Run Code Online (Sandbox Code Playgroud) 我有一个 Flutter 小部件,它接受用户输入并使用自定义画家绘制到画布上:
class SPPoint {
final Point point;
final double size;
SPPoint(this.point, this.size);
String toString() => "SPPoint $point $size";
}
class SignaturePadPainter extends CustomPainter {
final List<SPPoint> allPoints;
final SignaturePadOptions opts;
Canvas _lastCanvas;
Size _lastSize;
SignaturePadPainter(this.allPoints, this.opts);
ui.Image getPng() {
if (_lastCanvas == null) {
return null;
}
if (_lastSize == null) {
return null;
}
var recorder = new ui.PictureRecorder();
var origin = new Offset(0.0, 0.0);
var paintBounds = new Rect.fromPoints(_lastSize.topLeft(origin), _lastSize.bottomRight(origin));
var canvas = new Canvas(recorder, paintBounds); …
Run Code Online (Sandbox Code Playgroud)