-
-
Save mravn-google/029930ddb613b00b6f5df7179d76fdc4 to your computer and use it in GitHub Desktop.
Final main.dart working with bar charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:flutter/animation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'bar.dart'; | |
void main() { | |
runApp(MaterialApp(home: ChartPage())); | |
} | |
class ChartPage extends StatefulWidget { | |
@override | |
ChartPageState createState() => ChartPageState(); | |
} | |
class ChartPageState extends State<ChartPage> with TickerProviderStateMixin { | |
final random = Random(); | |
AnimationController animation; | |
BarChartTween tween; | |
@override | |
void initState() { | |
super.initState(); | |
animation = AnimationController( | |
duration: const Duration(milliseconds: 300), | |
vsync: this, | |
); | |
tween = BarChartTween( | |
BarChart.empty(), | |
BarChart.random(random), | |
); | |
animation.forward(); | |
} | |
@override | |
void dispose() { | |
animation.dispose(); | |
super.dispose(); | |
} | |
void changeData() { | |
setState(() { | |
tween = BarChartTween( | |
tween.evaluate(animation), | |
BarChart.random(random), | |
); | |
animation.forward(from: 0.0); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: CustomPaint( | |
size: Size(200.0, 100.0), | |
painter: BarChartPainter(tween.animate(animation)), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.refresh), | |
onPressed: changeData, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment