Skip to content

Instantly share code, notes, and snippets.

@mravn-google
Last active August 17, 2018 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mravn-google/029930ddb613b00b6f5df7179d76fdc4 to your computer and use it in GitHub Desktop.
Save mravn-google/029930ddb613b00b6f5df7179d76fdc4 to your computer and use it in GitHub Desktop.
Final main.dart working with bar charts
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