5
5
import 'package:flutter/material.dart' ;
6
6
7
7
class MultiWidgetConstructTable extends StatefulWidget {
8
- const MultiWidgetConstructTable (this .column , this .row , {Key key})
8
+ const MultiWidgetConstructTable (this .columnCount , this .rowCount , {Key key})
9
9
: super (key: key);
10
10
11
- final int column ;
12
- final int row ;
11
+ final int columnCount ;
12
+ final int rowCount ;
13
13
14
14
@override
15
15
_MultiWidgetConstructTableState createState () =>
@@ -24,82 +24,76 @@ class _MultiWidgetConstructTableState extends State<MultiWidgetConstructTable>
24
24
Colors .cyan, Colors .lightBlue, Colors .blue, Colors .indigo, Colors .purple,
25
25
];
26
26
int counter = 0 ;
27
- Color baseColor = colorList[0 ][900 ];
28
27
29
- AnimationController controller;
30
- CurvedAnimation curve;
28
+ AnimationController _controller;
31
29
32
30
@override
33
31
void initState () {
34
32
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 ();
58
39
}
59
40
60
41
@override
61
42
void dispose () {
62
- controller .dispose ();
43
+ _controller .dispose ();
63
44
super .dispose ();
64
45
}
65
46
66
47
@override
67
48
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
+ ),
99
93
),
100
94
),
101
- ),
102
- ) ,
95
+ );
96
+ } ,
103
97
);
104
98
}
105
99
}
0 commit comments