Skip to content

Commit 59aed97

Browse files
authored
Simplify the animation control for macrobenchmarks test case (flutter#60412)
* Simplify the animation control * Using AnimatedBuilder
1 parent 839dc27 commit 59aed97

File tree

3 files changed

+65
-63
lines changed

3 files changed

+65
-63
lines changed

dev/benchmarks/macrobenchmarks/lib/src/cubic_bezier.dart

+4
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ class AnimatedBezierState extends State<AnimatedBezier>
312312
super.initState();
313313
controller = AnimationController(
314314
vsync: this, duration: const Duration(milliseconds: 1000));
315+
// Animations are typically implemented using the AnimatedBuilder widget.
316+
// This code uses a manual listener for historical reasons and will remain
317+
// in order to preserve compatibility with the history of measurements for
318+
// this benchmark.
315319
curve = CurvedAnimation(parent: controller, curve: Curves.linear)
316320
..addListener(() {
317321
setState(() {});

dev/benchmarks/macrobenchmarks/lib/src/cull_opacity.dart

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class _CullOpacityPageState extends State<CullOpacityPage> with SingleTickerProv
1818
super.initState();
1919

2020
_controller = AnimationController(vsync: this, duration: const Duration(seconds: 2));
21+
// Animations are typically implemented using the AnimatedBuilder widget.
22+
// This code uses a manual listener for historical reasons and will remain
23+
// in order to preserve compatibility with the history of measurements for
24+
// this benchmark.
2125
_offsetY = Tween<double>(begin: 0, end: -1000.0).animate(_controller)..addListener((){
2226
setState(() {});
2327
});

dev/benchmarks/macrobenchmarks/lib/src/multi_widget_construction.dart

+57-63
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import 'package:flutter/material.dart';
66

77
class MultiWidgetConstructTable extends StatefulWidget {
8-
const MultiWidgetConstructTable(this.column, this.row, {Key key})
8+
const MultiWidgetConstructTable(this.columnCount, this.rowCount, {Key key})
99
: super(key: key);
1010

11-
final int column;
12-
final int row;
11+
final int columnCount;
12+
final int rowCount;
1313

1414
@override
1515
_MultiWidgetConstructTableState createState() =>
@@ -24,82 +24,76 @@ class _MultiWidgetConstructTableState extends State<MultiWidgetConstructTable>
2424
Colors.cyan, Colors.lightBlue, Colors.blue, Colors.indigo, Colors.purple,
2525
];
2626
int counter = 0;
27-
Color baseColor = colorList[0][900];
2827

29-
AnimationController controller;
30-
CurvedAnimation curve;
28+
AnimationController _controller;
3129

3230
@override
3331
void initState() {
3432
super.initState();
35-
controller = AnimationController(
36-
vsync: this, duration: const Duration(milliseconds: 10000));
37-
curve = CurvedAnimation(parent: controller, curve: Curves.linear)
38-
..addListener(() {
39-
final double colorPosition = curve.value;
40-
final int c1Position = (colorPosition * (colorList.length + 1)).floor();
41-
final Color c1 = colorList[c1Position % colorList.length][900];
42-
final Color c2 = colorList[(c1Position + 1) % colorList.length][900];
43-
setState(() {
44-
baseColor = Color.lerp(
45-
c1, c2, colorPosition * (colorList.length + 1) - c1Position);
46-
});
47-
})
48-
..addStatusListener((AnimationStatus state) {
49-
if (state == AnimationStatus.completed) {
50-
controller.reverse();
51-
} else if (state == AnimationStatus.dismissed) {
52-
controller.reset();
53-
controller.forward();
54-
}
55-
});
56-
57-
controller.forward();
33+
_controller = AnimationController(
34+
vsync: this,
35+
duration: const Duration(milliseconds: 10000),
36+
lowerBound: 0,
37+
upperBound: colorList.length + 1.0,
38+
)..repeat();
5839
}
5940

6041
@override
6142
void dispose() {
62-
controller.dispose();
43+
_controller.dispose();
6344
super.dispose();
6445
}
6546

6647
@override
6748
Widget build(BuildContext context) {
68-
final int totalLength = widget.row * widget.column;
69-
final int widgetCounter = counter * totalLength;
70-
final double height = MediaQuery.of(context).size.height / widget.column;
71-
counter++;
72-
return Scaffold(
73-
body: Table(
74-
children: List<TableRow>.generate(
75-
widget.row,
76-
(int row) => TableRow(
77-
children: List<Widget>.generate(
78-
widget.column,
79-
(int column) {
80-
final int label = row * widget.column + column;
81-
return counter % 2 == 0
82-
? Container(
83-
// This key forces rebuilding the element
84-
key: ValueKey<int>(widgetCounter + label),
85-
color: Color.lerp(
86-
Colors.white, baseColor, label / totalLength),
87-
child: Text('${widgetCounter + label}'),
88-
constraints: BoxConstraints.expand(height: height),
89-
)
90-
: MyContainer(
91-
// This key forces rebuilding the element
92-
key: ValueKey<int>(widgetCounter + label),
93-
color: Color.lerp(
94-
Colors.white, baseColor, label / totalLength),
95-
child: Text('${widgetCounter + label}'),
96-
constraints: BoxConstraints.expand(height: height),
97-
);
98-
},
49+
return AnimatedBuilder(
50+
animation: _controller,
51+
builder: (BuildContext context, _) {
52+
final int totalLength = widget.rowCount * widget.columnCount;
53+
final int widgetCounter = counter * totalLength;
54+
final double height = MediaQuery.of(context).size.height / widget.rowCount;
55+
final double colorPosition = _controller.value;
56+
final int c1Position = colorPosition.floor();
57+
final Color c1 = colorList[c1Position % colorList.length][900];
58+
final Color c2 = colorList[(c1Position + 1) % colorList.length][900];
59+
final Color baseColor = Color.lerp(c1, c2, colorPosition - c1Position);
60+
counter++;
61+
return Scaffold(
62+
body: Table(
63+
children: List<TableRow>.generate(
64+
widget.rowCount,
65+
(int row) => TableRow(
66+
children: List<Widget>.generate(
67+
widget.columnCount,
68+
(int column) {
69+
final int label = row * widget.columnCount + column;
70+
// This implementation rebuild the widget tree for every
71+
// frame, and is intentionally designed of poor performance
72+
// for benchmark purposes.
73+
return counter % 2 == 0
74+
? Container(
75+
// This key forces rebuilding the element
76+
key: ValueKey<int>(widgetCounter + label),
77+
color: Color.lerp(
78+
Colors.white, baseColor, label / totalLength),
79+
child: Text('${widgetCounter + label}'),
80+
constraints: BoxConstraints.expand(height: height),
81+
)
82+
: MyContainer(
83+
// This key forces rebuilding the element
84+
key: ValueKey<int>(widgetCounter + label),
85+
color: Color.lerp(
86+
Colors.white, baseColor, label / totalLength),
87+
child: Text('${widgetCounter + label}'),
88+
constraints: BoxConstraints.expand(height: height),
89+
);
90+
},
91+
),
92+
),
9993
),
10094
),
101-
),
102-
),
95+
);
96+
},
10397
);
10498
}
10599
}

0 commit comments

Comments
 (0)