Unverified Commit 59aed976 authored by Ming Lyu (CareF)'s avatar Ming Lyu (CareF) Committed by GitHub

Simplify the animation control for macrobenchmarks test case (#60412)

* Simplify the animation control

* Using AnimatedBuilder
parent 839dc27e
...@@ -312,6 +312,10 @@ class AnimatedBezierState extends State<AnimatedBezier> ...@@ -312,6 +312,10 @@ class AnimatedBezierState extends State<AnimatedBezier>
super.initState(); super.initState();
controller = AnimationController( controller = AnimationController(
vsync: this, duration: const Duration(milliseconds: 1000)); vsync: this, duration: const Duration(milliseconds: 1000));
// Animations are typically implemented using the AnimatedBuilder widget.
// This code uses a manual listener for historical reasons and will remain
// in order to preserve compatibility with the history of measurements for
// this benchmark.
curve = CurvedAnimation(parent: controller, curve: Curves.linear) curve = CurvedAnimation(parent: controller, curve: Curves.linear)
..addListener(() { ..addListener(() {
setState(() {}); setState(() {});
......
...@@ -18,6 +18,10 @@ class _CullOpacityPageState extends State<CullOpacityPage> with SingleTickerProv ...@@ -18,6 +18,10 @@ class _CullOpacityPageState extends State<CullOpacityPage> with SingleTickerProv
super.initState(); super.initState();
_controller = AnimationController(vsync: this, duration: const Duration(seconds: 2)); _controller = AnimationController(vsync: this, duration: const Duration(seconds: 2));
// Animations are typically implemented using the AnimatedBuilder widget.
// This code uses a manual listener for historical reasons and will remain
// in order to preserve compatibility with the history of measurements for
// this benchmark.
_offsetY = Tween<double>(begin: 0, end: -1000.0).animate(_controller)..addListener((){ _offsetY = Tween<double>(begin: 0, end: -1000.0).animate(_controller)..addListener((){
setState(() {}); setState(() {});
}); });
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class MultiWidgetConstructTable extends StatefulWidget { class MultiWidgetConstructTable extends StatefulWidget {
const MultiWidgetConstructTable(this.column, this.row, {Key key}) const MultiWidgetConstructTable(this.columnCount, this.rowCount, {Key key})
: super(key: key); : super(key: key);
final int column; final int columnCount;
final int row; final int rowCount;
@override @override
_MultiWidgetConstructTableState createState() => _MultiWidgetConstructTableState createState() =>
...@@ -24,82 +24,76 @@ class _MultiWidgetConstructTableState extends State<MultiWidgetConstructTable> ...@@ -24,82 +24,76 @@ class _MultiWidgetConstructTableState extends State<MultiWidgetConstructTable>
Colors.cyan, Colors.lightBlue, Colors.blue, Colors.indigo, Colors.purple, Colors.cyan, Colors.lightBlue, Colors.blue, Colors.indigo, Colors.purple,
]; ];
int counter = 0; int counter = 0;
Color baseColor = colorList[0][900];
AnimationController controller; AnimationController _controller;
CurvedAnimation curve;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
controller = AnimationController( _controller = AnimationController(
vsync: this, duration: const Duration(milliseconds: 10000)); vsync: this,
curve = CurvedAnimation(parent: controller, curve: Curves.linear) duration: const Duration(milliseconds: 10000),
..addListener(() { lowerBound: 0,
final double colorPosition = curve.value; upperBound: colorList.length + 1.0,
final int c1Position = (colorPosition * (colorList.length + 1)).floor(); )..repeat();
final Color c1 = colorList[c1Position % colorList.length][900];
final Color c2 = colorList[(c1Position + 1) % colorList.length][900];
setState(() {
baseColor = Color.lerp(
c1, c2, colorPosition * (colorList.length + 1) - c1Position);
});
})
..addStatusListener((AnimationStatus state) {
if (state == AnimationStatus.completed) {
controller.reverse();
} else if (state == AnimationStatus.dismissed) {
controller.reset();
controller.forward();
}
});
controller.forward();
} }
@override @override
void dispose() { void dispose() {
controller.dispose(); _controller.dispose();
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final int totalLength = widget.row * widget.column; return AnimatedBuilder(
final int widgetCounter = counter * totalLength; animation: _controller,
final double height = MediaQuery.of(context).size.height / widget.column; builder: (BuildContext context, _) {
counter++; final int totalLength = widget.rowCount * widget.columnCount;
return Scaffold( final int widgetCounter = counter * totalLength;
body: Table( final double height = MediaQuery.of(context).size.height / widget.rowCount;
children: List<TableRow>.generate( final double colorPosition = _controller.value;
widget.row, final int c1Position = colorPosition.floor();
(int row) => TableRow( final Color c1 = colorList[c1Position % colorList.length][900];
children: List<Widget>.generate( final Color c2 = colorList[(c1Position + 1) % colorList.length][900];
widget.column, final Color baseColor = Color.lerp(c1, c2, colorPosition - c1Position);
(int column) { counter++;
final int label = row * widget.column + column; return Scaffold(
return counter % 2 == 0 body: Table(
? Container( children: List<TableRow>.generate(
// This key forces rebuilding the element widget.rowCount,
key: ValueKey<int>(widgetCounter + label), (int row) => TableRow(
color: Color.lerp( children: List<Widget>.generate(
Colors.white, baseColor, label / totalLength), widget.columnCount,
child: Text('${widgetCounter + label}'), (int column) {
constraints: BoxConstraints.expand(height: height), final int label = row * widget.columnCount + column;
) // This implementation rebuild the widget tree for every
: MyContainer( // frame, and is intentionally designed of poor performance
// This key forces rebuilding the element // for benchmark purposes.
key: ValueKey<int>(widgetCounter + label), return counter % 2 == 0
color: Color.lerp( ? Container(
Colors.white, baseColor, label / totalLength), // This key forces rebuilding the element
child: Text('${widgetCounter + label}'), key: ValueKey<int>(widgetCounter + label),
constraints: BoxConstraints.expand(height: height), color: Color.lerp(
); Colors.white, baseColor, label / totalLength),
}, child: Text('${widgetCounter + label}'),
constraints: BoxConstraints.expand(height: height),
)
: MyContainer(
// This key forces rebuilding the element
key: ValueKey<int>(widgetCounter + label),
color: Color.lerp(
Colors.white, baseColor, label / totalLength),
child: Text('${widgetCounter + label}'),
constraints: BoxConstraints.expand(height: height),
);
},
),
),
), ),
), ),
), );
), },
); );
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment