-
-
Save mravn-google/ec766e03aad1f4cca14cdae3e46170fa to your computer and use it in GitHub Desktop.
Single bar, animated.
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; | |
BarTween tween; | |
@override | |
void initState() { | |
super.initState(); | |
animation = AnimationController( | |
duration: const Duration(milliseconds: 300), | |
vsync: this, | |
); | |
tween = BarTween(Bar(0.0), Bar(50.0)); | |
animation.forward(); | |
} | |
@override | |
void dispose() { | |
animation.dispose(); | |
super.dispose(); | |
} | |
void changeData() { | |
setState(() { | |
tween = BarTween( | |
tween.evaluate(animation), | |
Bar(random.nextDouble() * 100.0), | |
); | |
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