1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
testWidgets('Baseline - control test', (WidgetTester tester) async {
await tester.pumpWidget(
const Center(
child: DefaultTextStyle(
style: TextStyle(
fontFamily: 'Ahem',
fontSize: 100.0,
),
child: Text('X', textDirection: TextDirection.ltr),
),
),
);
expect(tester.renderObject<RenderBox>(find.text('X')).size, const Size(100.0, 100.0));
});
testWidgets('Baseline - position test', (WidgetTester tester) async {
await tester.pumpWidget(
const Center(
child: Baseline(
baseline: 180.0,
baselineType: TextBaseline.alphabetic,
child: DefaultTextStyle(
style: TextStyle(
fontFamily: 'Ahem',
fontSize: 100.0,
),
child: Text('X', textDirection: TextDirection.ltr),
),
),
),
);
expect(tester.renderObject<RenderBox>(find.text('X')).size, const Size(100.0, 100.0));
expect(tester.renderObject<RenderBox>(find.byType(Baseline)).size,
within<Size>(from: const Size(100.0, 200.0), distance: 0.001));
});
testWidgets('Chip caches baseline', (WidgetTester tester) async {
int calls = 0;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Baseline(
baseline: 100.0,
baselineType: TextBaseline.alphabetic,
child: Chip(
label: BaselineDetector(() {
calls += 1;
}),
),
),
),
),
);
expect(calls, 1);
await tester.pump();
expect(calls, 1);
tester.renderObject<RenderBaselineDetector>(find.byType(BaselineDetector)).dirty();
await tester.pump();
expect(calls, 2);
});
testWidgets('ListTile caches baseline', (WidgetTester tester) async {
int calls = 0;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Baseline(
baseline: 100.0,
baselineType: TextBaseline.alphabetic,
child: ListTile(
title: BaselineDetector(() {
calls += 1;
}),
),
),
),
),
);
expect(calls, 1);
await tester.pump();
expect(calls, 1);
tester.renderObject<RenderBaselineDetector>(find.byType(BaselineDetector)).dirty();
await tester.pump();
expect(calls, 2);
});
testWidgets('LayoutBuilder returns child\'s baseline', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Baseline(
baseline: 180.0,
baselineType: TextBaseline.alphabetic,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return BaselineDetector(() {});
},
),
),
),
),
);
expect(tester.getRect(find.byType(BaselineDetector)).top, 160.0);
});
}
class BaselineDetector extends LeafRenderObjectWidget {
const BaselineDetector(this.callback, { Key? key }) : super(key: key);
final VoidCallback callback;
@override
RenderBaselineDetector createRenderObject(BuildContext context) => RenderBaselineDetector(callback);
@override
void updateRenderObject(BuildContext context, RenderBaselineDetector renderObject) {
renderObject.callback = callback;
}
}
class RenderBaselineDetector extends RenderBox {
RenderBaselineDetector(this.callback);
VoidCallback callback;
@override
bool get sizedByParent => true;
@override
double computeMinIntrinsicWidth(double height) => 0.0;
@override
double computeMaxIntrinsicWidth(double height) => 0.0;
@override
double computeMinIntrinsicHeight(double width) => 0.0;
@override
double computeMaxIntrinsicHeight(double width) => 0.0;
@override
double computeDistanceToActualBaseline(TextBaseline baseline) {
if (callback != null)
callback();
return 20.0;
}
void dirty() {
markNeedsLayout();
}
@override
void performResize() {
size = constraints.smallest;
}
@override
void paint(PaintingContext context, Offset offset) { }
}